![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
How to update a single field in a capped collection in MongoDB?
To update, you need to use update() for the field whose collection is set with capped: true. Let us create a collection with documents −
> db.createCollection("Demo112", { capped : true, size : 14, max : 3 } ); { "ok" : 1 } > db.demo112.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef47a9fd5fd66da21447e") } > db.demo112.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef47e9fd5fd66da21447f") } > db.demo112.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef4919fd5fd66da214480") }
Display all documents from a collection with the help of find() method −
> db.demo112.find();
This will produce the following output −
{ "_id" : ObjectId("5e2ef47a9fd5fd66da21447e"), "Name" : "Chris" } { "_id" : ObjectId("5e2ef47e9fd5fd66da21447f"), "Name" : "David" } { "_id" : ObjectId("5e2ef4919fd5fd66da214480"), "Name" : "David" }
Following is the query to update a single field in a capped collection −
> db.demo112.update( ... {Name:"David"}, ... {$set:{Name: "Robert"}}, ... {multi:true} ... ) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo112.find();
This will produce the following output −
{ "_id" : ObjectId("5e2ef47a9fd5fd66da21447e"), "Name" : "Chris" } { "_id" : ObjectId("5e2ef47e9fd5fd66da21447f"), "Name" : "Robert" } { "_id" : ObjectId("5e2ef4919fd5fd66da214480"), "Name" : "Robert" }
Advertisements