
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to find if element exists in document - MongoDB?
To find if element exists in a MongoDB document, use MongoDB $exists. Let us create a collection with documents −
> db.demo497.insertOne({"details":[{"Name":"Chris"},{"Name":"Bob"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3cfb0f3fa88e22790d1") } > db.demo497.insertOne({"details":[{"Name":"Carol"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3d9b0f3fa88e22790d2") } > db.demo497.insertOne({"details":[{}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3e9b0f3fa88e22790d3") }
Display all documents from a collection with the help of find() method −
> db.demo497.find();
This will produce the following output −
{ "_id" : ObjectId("5e84b3cfb0f3fa88e22790d1"), "details" : [ { "Name" : "Chris" }, { "Name" : "Bob" } ] } { "_id" : ObjectId("5e84b3d9b0f3fa88e22790d2"), "details" : [ { "Name" : "Carol" } ] } { "_id" : ObjectId("5e84b3e9b0f3fa88e22790d3"), "details" : [ { } ] }
Following is the query to find if element exists in a document −
> db.demo497.find({$or:[ ... {"details.Name" : {$ne : "Carol"}}, ... {details: {$exists: false}} ... ] ... });
This will produce the following output −
{ "_id" : ObjectId("5e84b3cfb0f3fa88e22790d1"), "details" : [ { "Name" : "Chris" }, { "Name" : "Bob" } ] } { "_id" : ObjectId("5e84b3e9b0f3fa88e22790d3"), "details" : [ { } ] }
- Related Articles
- Return True if a document exists in MongoDB?
- Check if value exists for a field in a MongoDB document?
- How to find a certain element in the MongoDB embedded document?
- How to check if event exists on element in jQuery?
- How to check if onClick exists on element in jQuery?
- How to check if element exists in the visible DOM?
- How to check if Element exists in c# Selenium drivers?
- MongoDB query to add new array element in document
- Check if MongoDB database exists?
- How to apply a condition only if field exists in MongoDB?
- How to pull an array element (which is a document) in MongoDB?
- How to find if a directory exists in Python?
- Find largest document size in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How to replace substring in MongoDB document?

Advertisements