
- 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
MongoDB query to update tag
To update tag in MongoDB, use the update command. Let us create a collection with documents −
> db.demo713.insertOne( ... { ... tags: ... [ ... { ... id:101, ... Name:"Tag-1" ... }, ... { ... id:102, ... Name:"Tag-3" ... }, ... { ... id:103, ... Name:"Tag-3" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea8625a5d33e20ed1097b87") }
Display all documents from a collection with the help of find() method −
> db.demo713.find();
This will produce the following output −
{ "_id" : ObjectId("5ea8625a5d33e20ed1097b87"), "tags" : [ { "id" : 101, "Name" : "Tag-1" }, { "id" : 102, "Name" : "Tag-3" }, { "id" : 103, "Name" : "Tag-3" } ] }
Following is the query to update tag −
> db.demo713.update({"tags.id":102},{$set:{"tags.$.Name":"Tag-2"}},false,true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo713.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ea8625a5d33e20ed1097b87"), "tags" : [ { "id" : 101, "Name" : "Tag-1" }, { "id" : 102, "Name" : "Tag-2" }, { "id" : 103, "Name" : "Tag-3" } ] }
- Related Articles
- Update tag records in MongoDB quickly
- MongoDB query to update selected fields
- MongoDB query to update nested document
- MongoDB query to update only certain fields?
- MongoDB query to update the nested document?
- MongoDB query to update array with another field?
- MongoDB query to update an array using FindAndUpdate()?
- MongoDB query to update a MongoDB row with only the objectid
- MongoDB query to update all documents matching specific IDs
- MongoDB query to update array object in index N?
- MongoDB query to update a specific document from a collection
- How to update many documents with one query in MongoDB?
- Update multiple rows in a single MongoDB query?
- How to run MongoDB query to update only a specific field value?
- MongoDB query to update field and modify the data currently in column

Advertisements