Can I use the document field as value when building Mongo queries?

时间:2016-07-28 20:29:54

标签: mongodb mongodb-query

I have the following MongoDB collection.

[{A:1, B:2}, {A:1, B:1}]

Is there a way to use the property "B" something as the following?

db.myCollection.find({A: '$B'})

I read that there is an approach that calculates the diff of A and B, but what I really want to know is if I can reference document fields when matching documents. This is important for me to understand what I can do and what I cannot do.

1 个答案:

答案 0 :(得分:0)

我不相信有这么快的方法。如果您想这样做,您可以指定自定义$where,例如:

db.myCollection.find({"$where": "this.A == this.B"})

正如手册中所述,这需要为集合中的每个记录运行该Javascript代码,因此这不是世界上最快的查询。