
- 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
Work with $push in MongoDB
Let us create a collection with documents −
> db.demo738.insertOne({Subjects:["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ead696557bb72a10bcf0661") } > db.demo738.insertOne({Subjects:["MySQL","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ead696657bb72a10bcf0662") }
Display all documents from a collection with the help of find() method −
> db.demo738.find();
This will produce the following output −
{ "_id" : ObjectId("5ead696557bb72a10bcf0661"), "Subjects" : [ "C", "C++" ] } { "_id" : ObjectId("5ead696657bb72a10bcf0662"), "Subjects" : [ "MySQL", "PL/SQL" ] }
Following is the query to push −
>db.demo738.update({_id:ObjectId("5ead696657bb72a10bcf0662")},{$push:{"Subjects":"MongoDB"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo738.find();
This will produce the following output −
{ "_id" : ObjectId("5ead696557bb72a10bcf0661"), "Subjects" : [ "C", "C++" ] } { "_id" : ObjectId("5ead696657bb72a10bcf0662"), "Subjects" : [ "MySQL", "PL/SQL", "MongoDB" ] }
- Related Articles
- Updating an array with $push in MongoDB
- Push query results into variable with MongoDB?
- How to push value with for loop in MongoDB?
- MongoDB $push in nested array?
- How do I $set and $push in single update with MongoDB?
- Implement MongoDB $push in embedded document array?
- How to push an array in MongoDB?
- Push and slice multiple times in MongoDB?
- How to work with variables in MongoDB query
- How to work with Stored JavaScript in MongoDB?
- How to work Date query with ISODate in MongoDB?
- Cannot push into an array from MongoDB?
- How to push an element into array in MongoDB?
- MongoDB query to push document into an array
- Push new key element into subdocument of MongoDB?

Advertisements