
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find All Collections in MongoDB with Specific Field
Let us implement the above syntax in order to find all documents in MongoDB with field name “StudentFirstName”. The query is as follows −
> db.getCollectionNames().forEach(function(myCollectionName) { ... var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count(); ... if (frequency > 0) { ... print(myCollectionName); ... } ... });
This will produce the following output −
multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemo
Let us verify the removeKeyFieldsDemo collection has field with name “StudentFirstName” or not. Following is the query −
> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}});
This will produce the following output displaying the StudentFirstName field exist −
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
Advertisements