Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 在Mongo中,如何返回fieldA=fieldB的文档?_Mongodb_Find - Fatal编程技术网

Mongodb 在Mongo中,如何返回fieldA=fieldB的文档?

Mongodb 在Mongo中,如何返回fieldA=fieldB的文档?,mongodb,find,Mongodb,Find,查询返回两个文档: db.testData.find({x:5}); {u id:ObjectId(“529680a82ac8f8a788401688”),“x”:5,“y”:20,“z”:“hi”} {“_id”:ObjectId(“529690982ac8f8a78840169e”),“x”:5,“y”:5,“z”:“地址”} 如何指定条件x=y以返回最后一个文档?您必须使用(速度慢且不使用索引) 您的查询将如下所示: db.testData.find({ x: 5, $wher

查询返回两个文档:

db.testData.find({x:5}); {u id:ObjectId(“529680a82ac8f8a788401688”),“x”:5,“y”:20,“z”:“hi”} {“_id”:ObjectId(“529690982ac8f8a78840169e”),“x”:5,“y”:5,“z”:“地址”}

如何指定条件x=y以返回最后一个文档?

您必须使用(速度慢且不使用索引)

您的查询将如下所示:

db.testData.find({
   x: 5,
   $where: "this.x == this.y"
});
思考一下,看看你的问题。如果您知道x=5,您可以这样做:-)


这个问题已经被询问和回答了很多次。db.testData.find({$where:“This.x==This.y”});将给出正确答案。使用聚合框架的副本的解决方案可能比使用$where.Wow,2次向下投票要好得多!这太神奇了。这个解决方案可以在字段x=5上使用索引,然后它将遍历所有带有where的字段。它很容易写,也很容易理解。标记为重复的解决方案显然更难理解。
db.testData.find({x: 5, y : 5});