Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
MongoDB query to update a MongoDB row with only the objectid
Use UPDATE to update and SET to set the new values. Let us create a collection with documents −
> db.demo34.insertOne({"StudentFirstName":"Chris","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e1758b4cfb11e5c34d898cd")
}
> db.demo34.insertOne({"StudentFirstName":"David","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e1758bdcfb11e5c34d898ce")
}
> db.demo34.insertOne({"StudentFirstName":"Bob","StudentAge":20});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e1758c6cfb11e5c34d898cf")
}
Display all documents from a collection with the help of find() method −
> db.demo34.find();
This will produce the following output −
{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 }
{ "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "David", "StudentAge" : 23 }
{ "_id" : ObjectId("5e1758c6cfb11e5c34d898cf"), "StudentFirstName" : "Bob", "StudentAge" : 20 }
Following is the query to update MongoDB row −
> db.demo34.update({_id:ObjectId("5e1758bdcfb11e5c34d898ce")},
... {$set:{"StudentFirstName" : "Mike","StudentAge" : 19}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo34.find();
This will produce the following output −
{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 }
{ "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "Mike", "StudentAge" : 19 }
{ "_id" : ObjectId("5e1758c6cfb11e5c34d898cf"), "StudentFirstName" : "Bob", "StudentAge" : 20 } Advertisements
