- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Update in MongoDB and prevent overwrite?
Let us create a collection with documents −
> db.demo601.insertOne( ... { ... id:1, ... userDetails: ... { ... userName:"John", ... userMailId:"John@gmail.com" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e95ff5ced011c280a0905c7") } > > db.demo601.insertOne( { id:2, userDetails: { userName:"Carol", userMailId:"Carol@gmail.com" } } );{ "acknowledged" : true, "insertedId" : ObjectId("5e95ff71ed011c280a0905c8") }
Display all documents from a collection with the help of find() method −
> db.demo601.find();
This will produce the following output −
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" : { "userName" : "John", "userMailId" : "John@gmail.com" } } { "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" : { "userName" : "Carol", "userMailId" : "Carol@gmail.com" } }
Following is the query to update −
>db.demo601.update({_id:ObjectId("5e95ff71ed011c280a0905c8")},{$set:{userMailId:"Carol@yahoo.com"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo601.find();
This will produce the following output −
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" : { "userName" : "John", "userMailId" : "John@gmail.com" } } { "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" : { "userName" : "Carol", "userMailId" : "Carol@gmail.com" }, "userMailId" : "Carol@yahoo.com" }
Advertisements