Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何查找包含两个相等值的数组的文档?_Mongodb - Fatal编程技术网

Mongodb 如何查找包含两个相等值的数组的文档?

Mongodb 如何查找包含两个相等值的数组的文档?,mongodb,Mongodb,我有chats与参与者的收藏阵列 [{ "_id": ObjectId("5d12b2a10507cfe0bad6d93c"), "participants": [{ "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"), "firstname": "John", "lastname": "Anderson", "icon": "/assets

我有chats与参与者的收藏阵列

    [{
    "_id": ObjectId("5d12b2a10507cfe0bad6d93c"),
    "participants": [{
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        },
        {
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        }
    ]
}, {
    "_id": ObjectId("5d1124a50507cfe0baba7909"),
    "participants": [{
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        },
        {
            "_id": ObjectId("5ce54cb80507cfe0ba25d74b"),
            "firstname": "Milosh",
            "lastname": "Jersi",
            "icon": "/assets/images/avatars/small/3.jpg"
        }
    ]
}]
我自己去拿
req.db.collection('chats').findOne({'participants.\u id':{$all:[req.userID,new mongo.ObjectID(req.params.to)]})
其中userID也是ObjectID且等于

通常它有不同的参与者,但我们的用户也可以向自己发送消息,这在许多社交网络中是允许的。 所以在这种情况下,我们的用户“John Anderson”向他自己发送了一条消息,我们为它插入了聊天文档

现在我遇到了一个问题,如何获得具有相等数组值的文档

{'participants._id': { '$all': [ 5ce4af580507cfe0ba1c6f5b, 5ce4af580507cfe0ba1c6f5b] }}
// return every chat contains our id in atleast one field, but we need both to be equal
// same for $in

{'participants._id': { '$eq': [ 5ce4af580507cfe0ba1c6f5b, 5ce4af580507cfe0ba1c6f5b] }}
// return nothing

我还能做什么?

您可以使用聚合框架,使用
$group
阶段来实现这一点。首先,按
chat.\u id
分组,并使用
$addToSet
在新数组中仅保留唯一的用户,然后添加一个筛选器以仅保留一个参与者的文档:

db.collection.aggregate([
  {
    "$unwind": "$participants"
  },
  {
    "$group": {
      "_id": "$_id",
      "participants": {
        "$addToSet": "$participants._id"
      }
    }
  },
  {
    "$match": {
      "participants": {
        "$size": 1
      }
    }
  }
])
结果:

[
  {
    "_id": ObjectId("5d12b2a10507cfe0bad6d93c"),
    "participants": [
      ObjectId("5ce4af580507cfe0ba1c6f5b")
    ]
  }
]

你可以在网上试试:

你能用聊天数据分享你的收藏吗?我在帖子中更新了第一个代码块,是聊天收藏的数据,只是用聊天室中的用户数组来记录。谢谢,我觉得这么简单的任务有点麻烦,但可能mongo没有其他方法(但它也可以查找与一个参与者的所有聊天,如何按某些参与者[0]进行筛选。)?