- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Updating MongoDB collection for _id?
To update for _id, use $set in MongoDB. Let us create a collection with documents −
db.demo741.insertOne({SubjectName:"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5ead718657bb72a10bcf0672") } > db.demo741.insertOne({SubjectName:"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5ead718957bb72a10bcf0673") } > db.demo741.insertOne({SubjectName:"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5ead718e57bb72a10bcf0674") }
Display all documents from a collection with the help of find() method −
> db.demo741.find();
This will produce the following output −
{ "_id" : ObjectId("5ead718657bb72a10bcf0672"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5ead718957bb72a10bcf0673"), "SubjectName" : "C" } { "_id" : ObjectId("5ead718e57bb72a10bcf0674"), "SubjectName" : "Java" }
Following is the query to updating MongoDB for _id −
>db.demo741.update({_id:ObjectId("5ead718957bb72a10bcf0673")},{$set:{SubjectName:"PL/SQL"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo741.find();
This will produce the following output −
{ "_id" : ObjectId("5ead718657bb72a10bcf0672"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5ead718957bb72a10bcf0673"), "SubjectName" : "PL/SQL" } { "_id" : ObjectId("5ead718e57bb72a10bcf0674"), "SubjectName" : "Java" }
- Related Articles
- Retrieving specific documents from collection by _id in MongoDB
- Looping MongoDB collection for sorting
- Update _id field in MongoDB
- Updating data in MongoDB
- MongoDB query to group by _id
- Updating sub-object in MongoDB?
- Updating nested document in MongoDB
- Can MongoDB find() function display avoiding _id?
- How to update _id field in MongoDB?
- Get the array of _id in MongoDB?
- Updating Nested Embedded Documents in MongoDB?
- MongoDB syntax for updating an object inside an array within a document?
- Find the MongoDB collection size for name “Chris”
- Get execution stats in MongoDB for a collection
- How to search document in MongoDB by _id

Advertisements