我在 Mongo 中定义了一个集合,它是“支持”功能的列表。为了将另一个集合中的项目视为“兼容”,我需要获取该项目的功能列表并查看它们是否都在给定列表中。我已经在支持的功能集合中查询了功能列表,并将它们放在列表中。
例如,我的列表包含[A, B, C, D, E]
如果我的商品有 [A, B]
,则应退回,但如果有 [A, B, F]
,则不应退回。 $in运营商状态:
"If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array"
$all运营商状态:
"The $all operator selects the documents where the value of a field is an array that contains all the specified elements."
我的问题是该项目没有 $all 命令的完整功能集,我不希望它匹配 $in 命令的单个元素。我的查询看起来像这样(因为我已经从 functions 集合中检索了兼容函数的列表):
db.myColl.find({"compatibility": {"$in": ["A","B","C","D","E","F"]}})
或
db.myColl.find({"compatibility": {"$all": ["A","B","C","D","E","F"]}})
我认为我需要的是能够有效地反转查询并说出类似这样的话的东西(我知道这是无效的,我只是想说明一点):
db.myColl.find({["A","B","C","D","E","F"]: {"$all": "compatibility"}})
有什么想法吗?
最佳答案
我最近遇到了一个非常相似的问题并找到了解决方案:
db.myColl.find({"compatibility":{$not:{$elemMatch:{$nin:["A","B","C","D","E","F"]}}}})
虽然我不是 mongodb 方面的专家,但他们似乎应该为此操作添加一个原语。
祝你好运!
关于java - Mongodb如何查看字段的所有元素是否包含在超集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837932/