MongoDB中基于条件的查找

Posted

技术标签:

【中文标题】MongoDB中基于条件的查找【英文标题】:condition based lookup in mongodb 【发布时间】:2021-08-25 11:58:54 【问题描述】:

我是 MongoDB 新手,node.js 在 mongo 查找方面需要帮助。我想根据条件加入两个文档。我正在使用猫鼬、节点和快递。在Conversation 模型的聚合中,我想查找另一个模型User

这是用户1


    "_id": "0b3bb0bb2d78405fa941dd3e1da9574a",
    "status": true,
    "fullName": "Irtza",
    "mobileNumber": "+9200000000000",
    "createdAt": 
        "$date": "2021-05-05T11:39:00.875Z"
    ,
    "updatedAt": 
        "$date": "2021-06-04T11:10:53.103Z"
    ,
    "__v": 0,

这是用户2


    "_id": "51701df06d084003853cd0837d76606a",
    "status": true,
    "fullName": "Shehroz Ali",
    "mobileNumber": "+10000000000",
    "createdAt": 
        "$date": "2021-05-06T11:28:48.980Z"
    ,
    "updatedAt": 
        "$date": "2021-06-04T12:34:03.676Z"
    ,
    "__v": 0,

他们之间有对话


    "_id": "9a4d221ebbde4ba68b28783232aed28c",
    "userIds": ["0b3bb0bb2d78405fa941dd3e1da9574a", "51701df06d084003853cd0837d76606a"],
    "type": "private",
    "conversationName": "abc",
    "createdAt": 
        "$date": "2021-06-04T10:46:43.061Z"
    ,
    "updatedAt": 
        "$date": "2021-06-04T10:46:43.061Z"
    ,
    "__v": 0

因此,基于上述场景,我想查找/加入与其 ID 未传递给对话的用户的对话。

有一个基于userId 的请求userId 我想查找User 模型。同样在我的Conversation 中,我有一个字符串数组"userIds": ["0b3bb0bb2d78405fa941dd3e1da9574a", "51701df06d084003853cd0837d76606a"] 如果userIds.0userId 匹配,则使用userIds.1 查找,如果userIds.1userId 匹配,则使用userIds.0 查找这是我的代码:

this.aggregate([
                 $match:  userIds:  $all: [userId]   ,
                
                    $lookup: 
                        from: 'users',
                        localField: 'userIds.0',
                        foreignField: '_id',
                        as: 'user1',
                    
                ,
                 $unwind: "$user1" ,
                
                    $lookup: 
                        from: 'users',
                        localField: 'userIds.1',
                        foreignField: '_id',
                        as: 'user2',
                    
                ,
                 $unwind: "$user2" ,
                
                    $lookup: 
                        from: 'messages',
                        localField: '_id',
                        foreignField: 'conversationId',
                        as: 'messageBody',
                    
                ,
                 "$addFields": 
                    "messageBody": 
                        "$arrayElemAt": [ "$messageBody", -1 ]
                    
                ,
                 $unwind: "$messageBody" ,
            ]);

【问题讨论】:

你能添加一个对话和用户文档的例子吗? @NenadMilosavljevic 请立即查看。 我发布了我的答案。检查它是否适合您。 【参考方案1】:

您应该先使用$unwind,然后再使用$lookup。你可以这样做:

$match 过滤我们的对话,其中请求的userId 存在于userIds 数组中

$unwind 解构userIds 数组字段

$match 再次过滤仅请求的userId

$lookupuserId 替换为实际的用户文档

db.conversations.aggregate([
  
    "$match": 
      "userIds": "0b3bb0bb2d78405fa941dd3e1da9574a"
    
  ,
  
    "$unwind": "$userIds"
  ,
  
    "$match": 
      "userIds": 
        "$ne": "0b3bb0bb2d78405fa941dd3e1da9574a"
      
    
  ,
  
    "$lookup": 
      "from": "users",
      "localField": "userIds",
      "foreignField": "_id",
      "as": "userIds"
    
  
])

下面是工作示例:https://mongoplayground.net/p/I5GyY2XMLkb

【讨论】:

感谢您的回复,但我想加入 id 在 userIds 数组中不匹配的用户。 您可以在问题中添加您的预期输出吗? 如果“0b3bb0bb2d78405fa941dd3e1da9574a”在请求中,那么作为响应它给了我这个用户ID“51701df06d084003853cd0837d76606a”和用户详细信息。 我明白了。然后你可以添加$ne。我更新了我的答案和工作示例。你能检查它是否适合你吗?

以上是关于MongoDB中基于条件的查找的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB:查找没有子文档符合条件的文档

MongoDB命令查找单个文档并在更新中有条件语句

根据mongodb查询条件查找文档

使用 pymongo 在 mongoDB 中查找

如何使用多个条件查找查询在 MongoDB 中指定字段?

MongoDB 查找与使用聚合指定的任何条件匹配的文档