[英]MongoDB - find documents with couple conditions
Requirement: COUNT all documents in the collection WHERE objects.objectType equal to 'group' AND (objects.objectType NOT equal to 'person' && relation equal to 'Exposed_to')要求:计算集合中的所有文档,其中 objects.objectType 等于“group”并且(objects.objectType 不等于“person”&& relation 等于“Exposed_to”)
Expected: will return count of all documents WHERE objects.objectType equal to 'group' AND which does not contain any (objectType:person && realtion:Exposed_to) under 'objects' array.预期:将返回所有文档的计数,其中 objects.objectType 等于'group'并且不包含'objects'数组下的任何(objectType:person && realtion:Exposed_to)。
An example of matched document:匹配文档的示例:
"objects": [
{
"objectType": "organization",
"relation": "Exposed_to"
},
{
"objectType": "group",
"relation": "Exposed_to"
}
]
An example of document that shouldn't be counted:不应计算的文档示例:
"objects": [
{
"objectType": "person",
"relation": "Exposed_to"
},
{
"objectType": "group",
"relation": "Exposed_to"
}
]
i have tried the following query:我尝试了以下查询:
.count({
'objects.objectType': 'group',
'objects': {
$elemMatch: {
$and: [{'objectType' : {$ne: 'person'}, 'relation': 'Exposed_to'}]
}
}})
but it seems to be not working correctly.但它似乎无法正常工作。 i am not sure if i should use aggregation in this case- i also have less knowledge in this area.我不确定在这种情况下是否应该使用聚合——我在这方面的知识也较少。
i will be glad to get some help.我很乐意得到一些帮助。 thanks!谢谢!
Is it working for you?它对你有用吗? https://mongoplayground.net/p/Q-dj7_O_AJR https://mongoplayground.net/p/Q-dj7_O_AJR
This playground doesn't support count
, so I replace by find
这个游乐场不支持count
,所以我用find
代替
db.collection.find({
$and: [
{
"objects.objectType": "group"
},
{
objects: {
$not: {
"$elemMatch": {
"objectType": "person",
"relation": "Exposed_to"
}
}
}
}
]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.