
- 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 array with another field?
To update array with another field, use $push. Let us create a collection with documents −
> db.demo283.insertOne({"Name":"Chris","Status":["Active","Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ab97ddd099650a5401a75") }
Display all documents from a collection with the help of find() method −
> db.demo283.find();
This will produce the following output −
{ "_id" : ObjectId("5e4ab97ddd099650a5401a75"), "Name" : "Chris", "Status" : [ "Active", "Inactive" ] }
Following is the query to update array with another field −
> db.demo283.update({}, {$push: {Status:{$each:['Regular', 'NotRegular'], '$slice': -4}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo283.find();
This will produce the following output −
{ "_id" : ObjectId("5e4ab97ddd099650a5401a75"), "Name" : "Chris", "Status" : [ "Active", "Inactive", "Regular", "NotRegular" ]
- Related Articles
- Update MongoDB field using value of another field?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to update an array using FindAndUpdate()?
- Query a nested field within an array with MongoDB
- How to run MongoDB query to update only a specific field value?
- MongoDB query to update array object in index N?
- Update field in exact element array in MongoDB?
- Convert a field to an array using MongoDB update operation?
- MongoDB query to update field and modify the data currently in column
- MongoDB query to update tag
- MongoDB query to update a MongoDB row with only the objectid
- Convert a field to an array using update operation in MongoDB
- MongoDB query to match documents that contain an array field
- MongoDB query to update selected fields
- MongoDB query to update nested document

Advertisements