Search This Blog

2023/05/17

MongoDb:ReplaceOne

Today we will explore replaceOne() in mongoDb.

We are going to use collection "restaurant",so first make sure
their is no existing collection of this name for that we will
drop existing one with this name.

db.restaurant.drop()

Now to run our query we need some documents inside our colelction,
so lets add sample documents.

db.restaurant.insertMany(
[
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill",
"Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn",
"violations" : 0 },
{ "_id" : 4, "name" : "Central Perk Cafe", "Borough" : "New York" },
]
)

Replace One without Upsert:

db.restaurant.replaceOne(
{ "name": "Central Perk Cafe" },
{ "name": "Central Pork Cafe",
            "Borough": "Mumbai","state":"Maharshtra" }
);

Here mongo will find a document where { "name": "Central Perk Cafe" } and
replace the whole document with
{ "name": "Central Pork Cafe", "Borough": "Mumbai","state":"Maharshtra" } ,
it should be noted that _id fields will not get updated it will
continue to have old value.After Update the matching document becomes.

{
_id: 1,
name: 'Central Pork Cafe',
Borough: 'Mumbai',
state: 'Maharshtra'
}

Note:
Actually there are two documents matching filter condition
{ "name":"Central Perk Cafe" } but we are running replaceOne()
so only first matching document will be update others will
remain untouched.

Suppose we want to replace both matching documents then instead of
this replaceOne query we should use updateMany.

db.restaurant.updateMany(
{ "name": "Central Perk Cafe" },
{$set:{ "name": "Central Pork Cafe",
"Borough": "Mumbai","state":"Maharshtra" } }
);
This will replace both matching documents.

But if you try to run following query then it will give you an error

db.restaurant.updateMany(
{ "name": "Central Pork Cafe" },
{$set:{ "_id":0,"name": "Central Pork Cafe",
"Borough": "Mumbai","state":"Maharshtra" } }
);

as the _id property can only be set at the time of insertion if not done
at the time of insertion then mongodb create one on its own,once
created _id property can not be updated nor can you delete that _id field
only option is delete whole document.

Replace One With Upsert:
db.restaurant.replaceOne(
{ "name" : "Pizza Rat's Pizzariaa" },
{ "_id": 4, "name" : "Pizza Rat's Pizzaria",
"Borough" : "Manhattan", "violations" : 8 },
{ upsert: true }
);

Here their is no document to match filter condition
{ "name" : "Pizza Rat's Pizzaria" }
but we set upsert as true.so it will insert new document with
{ "_id": 4, "name" : "Pizza Rat's Pizzaria",
        "Borough" : "Manhattan", "violations" : 8 }

If in second parameter i.e.
{ "_id": 4, "name" : "Pizza Rat's Pizzaria",
        "Borough" : "Manhattan", "violations" : 8 }

we remove _id field then also _id field will get added but this time
    it will be having value from mongoDB default method i.e. objectId().

No comments:

Post a Comment