
- 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
Return True if a document exists in MongoDB?
Let us first create a collection. Following is the query to create a collection with documents
> db.documentExistsOrNotDemo.insertOne({"UserId":101,"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932bd330fd0aa0d2fe4cf") } > db.documentExistsOrNotDemo.insertOne({"UserId":102,"UserName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932c6330fd0aa0d2fe4d0") } > db.documentExistsOrNotDemo.insertOne({"UserId":102,"UserName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932ce330fd0aa0d2fe4d1") }
Following is the query to display all the documents from a collection with the help of find() method
> db.documentExistsOrNotDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9932bd330fd0aa0d2fe4cf"), "UserId" : 101, "UserName" : "John" } { "_id" : ObjectId("5c9932c6330fd0aa0d2fe4d0"), "UserId" : 102, "UserName" : "Chris" } { "_id" : ObjectId("5c9932ce330fd0aa0d2fe4d1"), "UserId" : 102, "UserName" : "Robert" }
Case 1: Following is the query that returns true if a document exists
> db.documentExistsOrNotDemo.find({"UserId":101}).count() > 0;
This will produce the following output
True
Case 2 Following is the query that returns false if a document does not exist
> db.documentExistsOrNotDemo.find({"UserId":110}).count() > 0;
This will produce the following output
False
- Related Articles
- Check if value exists for a field in a MongoDB document?
- How to find if element exists in document - MongoDB?
- Return specific MongoDB embedded document
- MongoDB query to return only embedded document?
- Check if MongoDB database exists?
- How to apply a condition only if field exists in MongoDB?
- How to return the position of a document relative to the collection in MongoDB?
- Drop Collection if already exists in MongoDB using Python
- Find a document with ObjectID in MongoDB?
- How to delete a document in MongoDB?
- Update only a single document in MongoDB
- Remove only a single document in MongoDB
- How to add a sub-document to sub-document array in MongoDB?
- Updating nested document in MongoDB
- Retrieving the first document in a MongoDB collection?

Advertisements