
- 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
Check for existing documents/embedded documents in MongoDB
To check for existing documents/embedded documents, use $exists in MongoDB. Let us create a collection with documents −
> db.demo322.insertOne( ... {'id':1001, ... 'details':[{'Score':10000,Name:"Bob"}, ... {'Score':98000,Name:"Sam"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113e2f8647eb59e56206c") } > db.demo322.insertOne( ... {'id':10002, ... 'details':[{'Score':9000}, ... {'Score':91000} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113faf8647eb59e56206d") }
Display all documents from a collection with the help of find() method −
> db.demo322.find();
This will produce the following output −
{ "_id" : ObjectId("5e5113e2f8647eb59e56206c"), "id" : 1001, "details" : [ { "Score" : 10000, "Name" : "Bob" }, { "Score" : 98000, "Name" : "Sam" } ] } { "_id" : ObjectId("5e5113faf8647eb59e56206d"), "id" : 10002, "details" : [ { "Score" : 9000 }, { "Score" : 91000 } ] }
Following is the query to check for existing documents/embedded documents −
> db.demo322.find({"details.Name":{$exists:true}}).count() > 0;
This will produce the following output −
True
- Related Articles
- MongoDB - Query embedded documents?
- Updating Nested Embedded Documents in MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Appending an entry in one to many embedded documents with MongoDB
- Query an array of embedded documents in MongoDB and push another?
- MongoDB bulk insert for documents
- Search for multiple documents in MongoDB?
- Aggregation in MongoDB for nested documents?
- How to update or modify the existing documents of a collection in MongoDB?
- Upsert many documents in MongoDB
- Check for Existing Document in MongoDB?
- Sort MongoDB documents in descending order
- Ignore null values in MongoDB documents
- Fetch specific multiple documents in MongoDB

Advertisements