Hãy để chúng tôi thực hiện cú pháp trên để tìm tất cả các tài liệu trong MongoDB với tên trường “StudentFirstName”. Truy vấn như sau -
> db.getCollectionNames().forEach(function(myCollectionName) {
... var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count();
... if (frequency > 0) {
... print(myCollectionName);
... }
... }); Điều này sẽ tạo ra kết quả sau -
multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemo
Hãy để chúng tôi xác minh bộ sưu tập removeKeyFieldsDemo có trường với tên “StudentFirstName” hay không. Sau đây là truy vấn -
> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}}); Điều này sẽ tạo ra kết quả sau hiển thị trường StudentFirstName tồn tại -
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }