【问题标题】:how to filter model.find query results with model.populate query results in mongoose?如何在猫鼬中使用 model.populate 查询结果过滤 model.find 查询结果?
【发布时间】:2015-11-28 17:39:09
【问题描述】:
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 数组中的州和国家/地区找到除我自己以外的所有用户。这可能与填充?

【问题讨论】:

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


    【解决方案1】:

    希望对您有所帮助。

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

    【讨论】:

    • 谢谢,但我想知道在我开始处理数组之前是否有办法用.populate 进一步过滤我的Users.find 查询结果。
    • 如果不行,我会用你的方法,谢谢。
    猜你喜欢
    • 2019-02-18
    • 2017-05-08
    • 2014-03-03
    • 1970-01-01
    • 2022-10-14
    • 2019-03-17
    • 1970-01-01
    • 2022-11-18
    • 2019-12-12
    相关资源
    最近更新 更多