如何使用mongoose中的model.populate查询结果过滤model.find查询结果?

时间:2015-09-03 03:00:02

标签: node.js mongodb mongoose mongodb-query mongoose-populate

var UserSchema = Schema (
{
    android_id: String,
    rooms: [{ type: Schema.Types.ObjectId, ref: 'Chatrooms'}],
    location: {country: String, state: String, coordinates: { latitude: String, longitude: String}}
});

var RoomSchema = Schema 
({
    Roomname: {type: String},
    ids: {type: Array},
});


Users
    .find({android_id: {$ne: userID}, 'location.state': LocationArray.state, 'location.country': LocationArray.country})
    .populate({
        path: 'rooms',
        match: {'rooms.ids': {$nin: [userID]}},
        select: 'ids'
     })
     .exec(function(err, found) {
     })

我想在一个州和国家中找到所有不包括我自己的用户,我不在他们的ids数组中。这可能与填充?

1 个答案:

答案 0 :(得分:2)

希望这可以帮到你。

Users
.find({android_id: {$ne: userID}, 'location.state': LocationArray.state, 'location.country': LocationArray.country})
.populate({
    path: 'rooms',
    match: {'ids': {$nin: [userID]}},
    select: 'ids'
 })
 .exec(function(err, found) {
 if(found){
      vettings = found.filter(function (doc) {
            return doc.ids;
        });
   }
 })