![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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