
- 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 Document in MongoDB?
You can use findOne() for this. Following is the syntax −
db.yourCollectionName.findOne({yourFieldName: 'yourValue'});
Let us create a collection with documents −
> db.checkExistingDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf90dac184d684e3fa265") } > db.checkExistingDemo.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf912ac184d684e3fa266") } > db.checkExistingDemo.insertOne({"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf916ac184d684e3fa267") } > db.checkExistingDemo.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf91bac184d684e3fa268") }
Display all documents from a collection with the help of find() method −
> db.checkExistingDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbdf90dac184d684e3fa265"), "StudentName" : "John" } { "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" } { "_id" : ObjectId("5cbdf916ac184d684e3fa267"), "StudentName" : "Sam" } { "_id" : ObjectId("5cbdf91bac184d684e3fa268"), "StudentName" : "Mike" }
Following is the query to check existing document −
> db.checkExistingDemo.findOne({StudentName: 'Carol'});
This will produce the following output −
{ "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" }
- Related Articles
- Check for existing documents/embedded documents in MongoDB
- Check if value exists for a field in a MongoDB document?
- How to update an existing document in MongoDB collection using Java?
- MongoDB - How to check for equality in collection and in embedded document?
- Include all existing fields and add new fields to document in MongoDB?
- How to update a MongoDB document without overwriting the existing one?
- MongoDB query for fields in embedded document?
- MongoDB find() query for nested document?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- How to change the password in MongoDB for existing user?
- Check for null in MongoDB?
- Working with MongoDB $sort for sub array document
- MongoDB query for exact match on multiple document fields
- MongoDB “$and” operator for subcollection to fetch a document?
- Updating nested document in MongoDB

Advertisements