[MongoDB] 全ての collection から特定の ObjectId の document を探すクエリ
MongoDB で全ての collection から特定の ObjectId の document を探すクエリをご紹介します。
特定の ObjectId から全 collection を走査して document を探すクエリ
var id = ObjectId('5a746d1757eaba024cefac5e'); var printDocById = function(id) { var collectionNames = db.getCollectionNames(); collectionNames.forEach(function(collectionName) { print(collectionName); var doc = db[collectionName].findOne({ _id: id }); if (doc) { printjson(doc); } }); }; printDocById(id); // 以下、実行結果です。 /** tags users { "_id" : ObjectId("5a746d1757eaba024cefac5e"), "name" : "hoge", "createdAt" : ISODate("2017-01-01T00:00:00Z") } **/ |
以上、どの collection か分からないけど ObjectId だけを手がかりに特定の document を見つけたい、現場からお送りしました。