
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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" }
- Related Articles
- Convert Collection to Capped in MongoDB
- MongoDB query to update each field of documents in collection with a formula?
- How to select a single field in MongoDB?
- Display only a single field from all the documents in a MongoDB collection
- Set max on MongoDB capped collection?
- How to check empty field in a MongoDB collection?
- Is it possible to use MongoDB capped collection?
- MongoDB query for capped sub-collection in an array
- Want to update inner field in a MongoDB
- How to Update multiple documents in a MongoDB collection using Java?
- How to update _id field in MongoDB?
- Update only a single document in MongoDB
- Invoke convertToCapped and convert an existing collection to capped in MongoDB
- MongoDB findOneAndUpdate() to update a single document
- How to update MongoDB collection using $toLower?

Advertisements