use*_*951 5 mongodb mongodb-query robo3t
我有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)
尝试这个:
db.getCollectionNames().forEach(function(collName) {
var doc = db.getCollection(collName).findOne({"_id" : "54d0232ef83ea4000d2c0610"});
if(doc != null) print(doc._id + " was found in " + collName);
});
Run Code Online (Sandbox Code Playgroud)
使用db.getCollection
编辑:您可以获得有关此问题的更多详细信息:在MongoDB中获取文档而无需指定集合
| 归档时间: |
|
| 查看次数: |
4789 次 |
| 最近记录: |