简体   繁体   中英

Sails.js Waterline Query

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?

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 :)

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM