查詢 MongoDB 中具有特定欄位的所有集合?


讓我們實施上述語法,以便查詢 MongoDB 中具有欄位名“StudentFirstName”的所有文件。查詢如下所示 −

> db.getCollectionNames().forEach(function(myCollectionName) {
...    var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count();
...    if (frequency > 0) {
...       print(myCollectionName);
...    }
... });

這將產生以下輸出 −

multiDimensionalArrayProjection
removeKeyFieldsDemo
stringOrIntegerQueryDemo

讓我們驗證一下 removeKeyFieldsDemo 集合是否具有名為“StudentFirstName”的欄位。查詢如下所示 −

> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}});

這將產生以下輸出,顯示 StudentFirstName 欄位存在 −

{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }

更新於:2019-07-30

939 次瀏覽

開啟你的 職業之路

Get certified by completing the course

Get Started
廣告
© . All rights reserved.