Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Only insert if a value is unique in MongoDB else update
You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.
Let us first create a collection with documents
> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Larry","StudentAge":22});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a633815e86fd1496b38a4")
}
> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Mike","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a634a15e86fd1496b38a5")
}
> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Sam","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a635015e86fd1496b38a6")
}
Following is the query to display all documents from a collection with the help of find() method
> db.onlyInsertIfValueIsUniqueDemo.find().pretty();
This will produce the following output
{
"_id" : ObjectId("5c9a633815e86fd1496b38a4"),
"StudentName" : "Larry",
"StudentAge" : 22
}
{
"_id" : ObjectId("5c9a634a15e86fd1496b38a5"),
"StudentName" : "Mike",
"StudentAge" : 21
}
{
"_id" : ObjectId("5c9a635015e86fd1496b38a6"),
"StudentName" : "Sam",
"StudentAge" : 24
}
Case 1: Following is the query when a value already exists. Since the value you are inserting already exists, it gets updated
> db.onlyInsertIfValueIsUniqueDemo.update({StudentName:"Mike"},{$set:{"StudentAge":27}},{ upsert: true});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Look at all the documents once again. Following is the query
> db.onlyInsertIfValueIsUniqueDemo.find().pretty();
This will produce the following output
{
"_id" : ObjectId("5c9a633815e86fd1496b38a4"),
"StudentName" : "Larry",
"StudentAge" : 22
}
{
"_id" : ObjectId("5c9a634a15e86fd1496b38a5"),
"StudentName" : "Mike",
"StudentAge" : 27
}
{
"_id" : ObjectId("5c9a635015e86fd1496b38a6"),
"StudentName" : "Sam",
"StudentAge" : 24
}
Case 2: Following is the query when a value is unique. Since the value you are inserting does not already exists, it gets inserted
>db.onlyInsertIfValueIsUniqueDemo.update({StudentName:"David"},{$set:{"StudentAge":25}},{ upsert: true});
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5c9a654ce628c11759caea54")
})
Look at the all document once again. Following is the query
> db.onlyInsertIfValueIsUniqueDemo.find().pretty();
This will produce the following output
{
"_id" : ObjectId("5c9a633815e86fd1496b38a4"),
"StudentName" : "Larry",
"StudentAge" : 22
}
{
"_id" : ObjectId("5c9a634a15e86fd1496b38a5"),
"StudentName" : "Mike",
"StudentAge" : 27
}
{
"_id" : ObjectId("5c9a635015e86fd1496b38a6"),
"StudentName" : "Sam",
"StudentAge" : 24
}
{
"_id" : ObjectId("5c9a654ce628c11759caea54"),
"StudentName" : "David",
"StudentAge" : 25
}