我有这样的收藏:
{
"_id" : ObjectId("546089aaf83a94de268a35c3"),
"parent" : {
"$ref" : "someRef",
"$id" : ObjectId("546089aaf83a94de268a35c3"),
"$db" : "someDB",
}
}
如何查找_id等于parent.id的所有记录?
尝试过 find({"parent.$id": "_id"})
但显然不可能是这样
最佳答案
此处的parent
包含 database reference 。因此我们需要调用引用的getter方法来获取它的对象ID。
以下查询可以获得预期的输出:
db.collection.find({
$where: function(){
return this._id.toString() == this.parent.getId().toString();
}
}).pretty()
关于MongoDB,查找属性id等于记录id的所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392804/