- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Search by property name for any document with that property in MongoDB?
You can use $ne operator for this. Let us first create a collection with documents −
> db.searchByPropertyName.insertOne({"FirstName":"Larry","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cbaf7af7219729fde21ddb5") } > db.searchByPropertyName.insertOne({"FirstName":null,"Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cbaf7b97219729fde21ddb6") } > db.searchByPropertyName.insertOne({"FirstName":"John","Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5cbaf7c57219729fde21ddb7") } > db.searchByPropertyName.insertOne({"FirstName":null,"Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5cbaf7d07219729fde21ddb8") } > db.searchByPropertyName.insertOne({"FirstName":"David","Age":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cbaf7df7219729fde21ddb9") }
Following is the query to display all documents from the collection with the help of find() prettyprint −
> db.searchByPropertyName.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbaf7af7219729fde21ddb5"), "FirstName" : "Larry", "Age" : 23 } { "_id" : ObjectId("5cbaf7b97219729fde21ddb6"), "FirstName" : null, "Age" : 21 } { "_id" : ObjectId("5cbaf7c57219729fde21ddb7"), "FirstName" : "John", "Age" : 22 } { "_id" : ObjectId("5cbaf7d07219729fde21ddb8"), "FirstName" : null, "Age" : 25 } { "_id" : ObjectId("5cbaf7df7219729fde21ddb9"), "FirstName" : "David", "Age" : 20 }
Following is the query to search by property name for any document with that property −
> db.searchByPropertyName.find({FirstName: { $ne : null } } ).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbaf7af7219729fde21ddb5"), "FirstName" : "Larry", "Age" : 23 } { "_id" : ObjectId("5cbaf7c57219729fde21ddb7"), "FirstName" : "John", "Age" : 22 } { "_id" : ObjectId("5cbaf7df7219729fde21ddb9"), "FirstName" : "David", "Age" : 20 }
- Related Articles
- How to search document in MongoDB by _id
- HTML DOM Input Search name Property
- Find the document by field name with a specific value in MongoDB?
- Search a string with special characters in a MongoDB document?
- Adding new property to each document in a large MongoDB collection?
- Perform nested document value search in MongoDB?
- Search a complex object by id property in JavaScript
- Update a MongoDB document with Student Id and Name
- Search for documents with similar arrays in MongoDB and order by similarity value
- Working with MongoDB $sort for sub array document
- HTML DOM Location search Property
- HTML DOM Anchor search Property
- Find document with array that contains a specific value in MongoDB
- Check for Existing Document in MongoDB?
- HTML DOM Input Search autocomplete Property

Advertisements