如何在数组结果中对$ lookup选项使用$ match条件

时间:2019-04-18 16:32:48

标签: mongodb mongoose aggregation

我有两个集合,一个集合looksee,另一个集合assignee_looksee,我想得出与条件looksee_assigne receipient_user相匹配的结果,并显示looksee数据,并显示结果,除匹配receipient_user外,其他assignee_looksee。

$ match条件在“ is_seen”中不起作用:数组结果为true

LookseeAssignee.aggregate([
{ '$match': {"receipient_user": mongoose.Types.ObjectId(sender_user_id)}},
{ "$lookup": {
    "from": "looksees",
    "localField": "looksee_id",
    "foreignField": "_id",  
    "as": "looksee_info",
}},

{ "$unwind": "$looksee_info" },


{ "$lookup": {
    "from": "looksee_assignees",
    "localField": "looksee_info._id", 
    "foreignField": "looksee_id",   
    "as": "looksee_assignee_info",
}},
// { "$unwind": "$looksee_assignee_info" },

{ "$match": { "looksee_assignee_info": { 
            $elemMatch: {"status":  1}
        } 
    }
}
], function(err, looksee_receive){
    res.send(looksee_receive);
});

结果是:

'looksee_info': {
  '_id': '5cb58a857b96bd0e413b4fd5',
  'is_abandoned': false,
  'abandoned_time': null,
  'sender_id': '5c4deaf53c45af6db6d9db30',
  '__v': 0
},
'looksee_assignee_info': [
  {
    '_id': '5cb58a857b96bd0e413b4fd6',
    'receipient_user': '5c63b8f507e125ab4f320e06',
    'is_seen': false,

  },
  {
    '_id': '5cb58a857b96bd0e413b4fd7',
    'receipient_user': '5c4deaf53c45af6db6d9db30',
    'is_seen': true,       
  },
  {
    '_id': '5cb58a857b96bd0e413b4fd8',
    'receipient_user': '5c6e472d88138d30a0104b6b',
    'is_seen': false,        
  }
]

0 个答案:

没有答案