MongoDB 使用 2 个列表的交集进行查询
MongoDB query using intersection of 2 lists
我正在做一个项目,我们使用 mongodb 4.4 作为我们的数据库。问题是我必须使用 mongo 查询语言进行过于复杂的查询。
这是数据库中数据的一个例子:
{
"neededQuantity": 100,
"auth": {
"groups": ["group-1"]
},
"createTime": "2022-02-03T14:17:25.809704600Z",
},
{
"neededQuantity": 120,
"auth": {
"groups": ["group-3"]
},
"createTime": "2022-02-02T14:17:25.809704600Z",
},
{
"neededQuantity": 150,
"auth": {
"groups": ["group-2","group-1"]
},
"createTime": "2022-02-01T14:17:25.809704600Z",
}
查询必须找到列表中至少有 1 个元素 auth.groups 与作为输入给出的另一个列表相同的文档。
如果输入列表是["group-1"],查询结果应该是:
{
"neededQuantity": 100,
"auth": {
"groups": ["group-1"]
},
"createTime": "2022-02-03T14:17:25.809704600Z",
},
{
"neededQuantity": 150,
"auth": {
"groups": ["group-2","group-1"]
},
"createTime": "2022-02-01T14:17:25.809704600Z",
}
我该怎么做?
非常感谢您。
你可以用 $in
db.collection.find({
"auth.groups": {$in: ["group-1"]}
})
有关 $in here
的更多详细信息
我正在做一个项目,我们使用 mongodb 4.4 作为我们的数据库。问题是我必须使用 mongo 查询语言进行过于复杂的查询。
这是数据库中数据的一个例子:
{
"neededQuantity": 100,
"auth": {
"groups": ["group-1"]
},
"createTime": "2022-02-03T14:17:25.809704600Z",
},
{
"neededQuantity": 120,
"auth": {
"groups": ["group-3"]
},
"createTime": "2022-02-02T14:17:25.809704600Z",
},
{
"neededQuantity": 150,
"auth": {
"groups": ["group-2","group-1"]
},
"createTime": "2022-02-01T14:17:25.809704600Z",
}
查询必须找到列表中至少有 1 个元素 auth.groups 与作为输入给出的另一个列表相同的文档。
如果输入列表是["group-1"],查询结果应该是:
{
"neededQuantity": 100,
"auth": {
"groups": ["group-1"]
},
"createTime": "2022-02-03T14:17:25.809704600Z",
},
{
"neededQuantity": 150,
"auth": {
"groups": ["group-2","group-1"]
},
"createTime": "2022-02-01T14:17:25.809704600Z",
}
我该怎么做?
非常感谢您。
你可以用 $in
db.collection.find({
"auth.groups": {$in: ["group-1"]}
})
有关 $in here
的更多详细信息