0

I want to do the following, lets say for a simple group chat application, we have models like

Message

  • room: {'model':Room}

Room

  • users: {'collection':User}

I want the following:

Given a user Bob, get all Messages in a room containing Bob.

What is the appropriate Waterline query for this?

zzz
  • 233
  • 3
  • 8

2 Answers2

0

I'm not sure I know what you mean about your model structure, but I guess you need something like this:

Message.find({ //Message is your Message model
  room:room_id, //or some other identifier
  content:{contains:'Bob'}
}).exec(function(error,result){
  if(error){
  // handle error here
  }else{
    var messages = result;
    //do your stuff.
  }
})

If I'm wrong about what you mean, please let me know :)

Giyya Pan
  • 43
  • 1
  • 4
0

I would suggest adding the both user's (Bob's) id and the room's id as a field in the Message model. For example this might be included in MessageModule.js:

userId : 'string',
roomId : 'string',

Your query to get all of Bob's messages in a room would be as simple as this:

Messages.find({userId: bobs_user_id, roomId: room_id},
function(err, messages){
    // all bob's messages from room room_id are available in messages
});