Skip to content

Instantly share code, notes, and snippets.

@shravan-kuchkula
Last active August 28, 2017 03:34
Show Gist options
  • Save shravan-kuchkula/c29ba5ef64020978a4d4fb637183a1b6 to your computer and use it in GitHub Desktop.
Save shravan-kuchkula/c29ba5ef64020978a4d4fb637183a1b6 to your computer and use it in GitHub Desktop.
Update operation from mongo shell. All 4 cases
# To insert the data in the people collection use 'insertOne.py' that I have given earlier
# Display the contents of the people collection
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Smith", "age" : 30, "profession" : "hacker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
# METHOD 1: Wholesale update, essentially replaces whole document.
> db.people.update({"name": "Smith"}, {"name": "Thompson", "salary": 50000})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
# METHOD 2: Using operators to add/modify/delete fields
> db.people.update({"name":"Alice"}, {"$set": {"age": 40}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
> db.people.update({"name":"Alice"}, {"$set": {"age": 41}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.update({"name":"Alice"}, {"$set": {"age": 41}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 41 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
> db.people.update({"name":"Alice"}, {"$inc": {"age": -1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
> db.people.update({"name":"Bob"}, {"$inc": {"age": 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
> db.people.update({"name":"Jones"}, {$unset: {"profession":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
# METHOD 3: UPSERTS concept. Note below that George does not exist in the collection yet.
# So adding George without the upsert option does not do anything.
> db.people.update({"name": "George"}, {$set: {age: 40}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
# SPECIFY upsert:true to insert the document if not found
> db.people.update({"name": "George"}, {$set: {age: 40}}, {upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("59a369cd905729b7e7af5e5e")
})
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
{ "_id" : ObjectId("59a369cd905729b7e7af5e5e"), "name" : "George", "age" : 40 }
> db.people.update({"name": "George"}, {$set: {age: 43}}, {upsert:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
{ "_id" : ObjectId("59a369cd905729b7e7af5e5e"), "name" : "George", "age" : 43 }
>
>
>
# METHOD 4: Using bulk update, we need specify "multi":true otherwise, just one document will be updated.
> db.people.update({}, {$set : {"title": "Dr"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
{ "_id" : ObjectId("59a369cd905729b7e7af5e5e"), "name" : "George", "age" : 43 }
> db.people.update({}, {$set : {"title": "Dr"}}, {multi:true})
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 9 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40, "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1, "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie", "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave", "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar", "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred", "title" : "Dr" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42, "title" : "Dr" }
{ "_id" : ObjectId("59a369cd905729b7e7af5e5e"), "name" : "George", "age" : 43, "title" : "Dr" }
> db.people.update({}, {$unset : {"title": "Dr"}}, {multi:true})
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 10 })
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice", "age" : 40 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob", "age" : 1 }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bc"), "name" : "Edgar" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bd"), "name" : "Fred" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1be"), "name" : 42 }
{ "_id" : ObjectId("59a369cd905729b7e7af5e5e"), "name" : "George", "age" : 43 }
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment