MongoDB的简单查询
问题描述:
MongoDB的查询问题:MongoDB的简单查询
parent = {
"_id": ObjectId("1"),
"children": [ObjectId("11"), ObjectId("12"), ObjectId("13")]
}
我想,如果给定的ID匹配任何孩子数组中返回整个父文档。
> db.parent.find({"children": ObjectId("11")})
不返回任何
TIA, 埃里克
答
您需要首先get
您db
这允许节点知道你在说什么db
。我用简单的_db = db.get('parent')
来做到这一点。 现在您可以尝试使用您的ObjectId
搜索数据库。与ObjectId
我相信你可以只搜索11
,因为它只是一个String
。如果
_db = db.get('parent')
_db.find({'children': '11'}, function(err, doc){
if(!err){
...
Docs will return the whole query
}else{
...
}
}
如果只是在寻找11
不工作,然后尝试使用ObjectId
。
现在,当我试图将ObjectId
添加到我的Mongotron
分贝时,它返回了Error : Argument passed in must be a single String of 12 bytes or a string of 24 hex characters Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters at new ObjectID
。你确定你的数据在db
?
我很积极的id是在儿童收藏。另外,不要在寻找节点代码,现在就使用mongo命令行。 – emiles
你有没有试过'db.parent.find({'children':'11'})'? –
对不起,想通了我的问题,小孩是另一个集合里的数组。所以下面的工作: – emiles