我有100多个集合的MongoDB的DATABSE。我试图找到一个对象,与已知的ObjectID,属于该数据库的一些(未知)集合。
我试着做:
db.getCollectionNames().forEach(function(collname) {
var object = db[collname].find({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
if(object._id !== undefined){
printjson("Found in " >> collname);
}
});
Run Code Online (Sandbox Code Playgroud)
...类似于这里建议'S:遍历所有蒙戈集合和执行查询
但是,脚本没有任何结果。
编辑:
当我这样做时,我得到了预期的结果Found!:
var object = db['rightcollection'].findOne({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
if(object !== null){
printjson("Found!");
}
Run Code Online (Sandbox Code Playgroud)
但是,下面的回报0(而不是没有返回为原为例):
db.getCollectionNames().forEach(function(collname) {
var object = db[collname].findOne({'_id' : ObjectId("54d0232ef83ea4000d2c0610")});
if(object !== null){
printjson("Found in " >> collname);
}
});
Run Code Online (Sandbox Code Playgroud)