![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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" ] }
Advertisements