查找 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
广告