
- 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
Update two separate arrays in a document with one update call in MongoDB?
You can use $push operator for this. Let us first create a collection with documents
>db.twoSeparateArraysDemo.insertOne({"StudentName":"Larry","StudentFirstGameScore":[98],"StudentSecondGameScore":[77]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152815e86fd1496b38b8") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"Mike","StudentFirstGameScore":[58],"StudentSecondGameScore":[78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152d15e86fd1496b38b9") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"David","StudentFirstGameScore":[65],"StudentSecondGameScore":[67]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b153315e86fd1496b38ba") }
Following is the query to display all documents from a collection with the help of find() method
> db.twoSeparateArraysDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b152815e86fd1496b38b8"), "StudentName" : "Larry", "StudentFirstGameScore" : [ 98 ], "StudentSecondGameScore" : [ 77 ] } { "_id" : ObjectId("5c9b152d15e86fd1496b38b9"), "StudentName" : "Mike", "StudentFirstGameScore" : [ 58 ], "StudentSecondGameScore" : [ 78 ] } { "_id" : ObjectId("5c9b153315e86fd1496b38ba"), "StudentName" : "David", "StudentFirstGameScore" : [ 65 ], "StudentSecondGameScore" : [ 67 ] }
Following is the query to push two separate arrays in one update call in MongoDB
> db.twoSeparateArraysDemo.update({_id:ObjectId("5c9b152d15e86fd1496b38b9")}, { $push : { StudentFirstGameScore : 45, StudentSecondGameScore : 99}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the value is pushed in two separate arrays or not
> db.twoSeparateArraysDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b152815e86fd1496b38b8"), "StudentName" : "Larry", "StudentFirstGameScore" : [ 98 ], "StudentSecondGameScore" : [ 77 ] } { "_id" : ObjectId("5c9b152d15e86fd1496b38b9"), "StudentName" : "Mike", "StudentFirstGameScore" : [ 58, 45 ], "StudentSecondGameScore" : [ 78, 99 ] } { "_id" : ObjectId("5c9b153315e86fd1496b38ba"), "StudentName" : "David", "StudentFirstGameScore" : [ 65 ], "StudentSecondGameScore" : [ 67 ] }
- Related Articles
- Update only a single document in MongoDB
- Update a MongoDB document with Student Id and Name
- Update only a specific value in a MongoDB document
- How to update a MongoDB document without overwriting the existing one?
- MongoDB findOneAndUpdate() to update a single document
- MongoDB query to update nested document
- Update a specific MongoDB document in array with $set and positional $ operator?
- Update array in MongoDB document by variable index?
- Display MongoDB with document and subdocument example and update
- MongoDB query to update the nested document?
- Getting unique values within two arrays in one MongoDB document
- Update a single list item of a MongoDB document?
- How to update document with marks value in MongoDB for student David
- How to update the _id of a MongoDB Document?
- How do you update a MongoDB document while replacing the entire document?

Advertisements