问题描述
我有以下json:
{
"a1": {"a": "b"},
"a2": {"a": "c"}
}
如何在同一文档中请求a1
和a2
不相等的所有文档?
How can I request all documents where a1
and a2
are not equal in the same document?
推荐答案
您可以使用 $where
:
You could use $where
:
db.myCollection.find( { $where: "this.a1.a != this.a2.a" } )
但是,请注意这不会很快,因为它必须启动Java脚本引擎并迭代每个文档并检查每个条件.
However, be aware that this won't be very fast, because it will have to spin up the java script engine and iterate each and every document and check the condition for each.
如果需要对大型集合进行查询,或者经常对大型集合进行查询,则最好引入非规范化标志,例如areEqual
.尽管如此,由于候选集仍然很大,因此此类低选择性字段仍无法获得良好的索引性能.
If you need to do this query for large collections, or very often, it's best to introduce a denormalized flag, like areEqual
. Still, such low-selectivity fields don't yield good index performance, because he candidate set is still large.
这篇关于使用同一文档中的字段进行Mongodb查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!