我有这样的收藏:
{
"_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()