
- 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
Insert records in MongoDB collection if it does not exist?
You can use update() function to insert records in MongoDB if it does not exist. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.insertIfNotExistsDemo.insertOne({"StudentName":"Mike","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c7eec7b559dd2396bcfbfc2") } > db.insertIfNotExistsDemo.insertOne({"StudentName":"Sam","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c7eec97559dd2396bcfbfc3") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.insertIfNotExistsDemo.find().pretty(); The following is the output: { "_id" : ObjectId("5c7eec7b559dd2396bcfbfc2"), "StudentName" : "Mike", "StudentAge" : 21 } { "_id" : ObjectId("5c7eec97559dd2396bcfbfc3"), "StudentName" : "Sam", "StudentAge" : 22 }
Here is the query to insert a record if it does not already exist −
> key = {"StudentName":"David"} { "StudentName" : "David" } > value = {"StudentName":"David","StudentAge":26} { "StudentName" : "David", "StudentAge" : 26 } > db.insertIfNotExistsDemo.update(key, value, upsert=true);
The following is the output −
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("5c7eecd4c743760e97af8261") })
Let us check all documents from a collection. The query is as follows −
> db.insertIfNotExistsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7eec7b559dd2396bcfbfc2"), "StudentName" : "Mike", "StudentAge" : 21 } { "_id" : ObjectId("5c7eec97559dd2396bcfbfc3"), "StudentName" : "Sam", "StudentAge" : 22 } { "_id" : ObjectId("5c7eecd4c743760e97af8261"), "StudentName" : "David", "StudentAge" : 26 }
Look at the sample output, the “StudentName”:”David” and “StudentAge”:26 has been inserted successfully.
- Related Articles
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- How to insert only those records that does not exist in a MySQL table?
- MySQL create user if it does not exist?
- Insert array where element does not exist else update it (with multiple conditions)?
- MongoDB query to determine if a specific value does not exist?
- Create view in MySQL only if it does not already exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- How to create a folder if it does not exist in C#?
- How can I create a python directory if it does not exist?
- How can I create a directory if it does not exist using Python?
- How to check if a table exists in MySQL and create if it does not already exist?
- Find records in MongoDB that does NOT match a condition?
- Does NOT EQUAL exist in MySQL?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- Add object to array in JavaScript if name does not already exist?

Advertisements