Contents of page >
Insert/ Put list of values in array in existing document/record in in collection(table) in MongoDB
$pushAll puts array of values in existing document. Let’s learn with example.
1) Before Insert/ Put list of values in array let’s create and query collection in MongoDB
Add more items to arrays in MongoDB.
First let's create new collection and insert documents in it before putting list of values in array in existing documents in mongoDB >
> db.testCol.insert( {"id":"1"})
|
Query/Read all documents of collection >
> db.testCol.find()
{ "_id" : ObjectId("58502ec51127dea5ece72763"), "id" : "1" }
>
|
2) Now, Let’s puts list of items in array in existing document >
> db.testCol.update( {"id":"1"}, {"$pushAll" : {arr1 : [1, 2, 3]}} )
|
Now let's display documents of collection after $pushall >
> db.testCol.find()
{ "_id" : ObjectId("58502ec51127dea5ece72763"), "id" : "1", "arr1" : [ 1, 2, 3 ] }
>
|
3) Let’s put some more list of items in array in existing record >
> db.testCol.update( {"id":"1"}, {"$pushAll" : {arr1 : [4, 5, 6]}} )
|
Now let's display documents of collection after $pushall >
> db.testCol.find()
{ "_id" : ObjectId("58502ec51127dea5ece72763"), "id" : "1", "arr1" : [ 1, 2, 3, 4, 5, 6 ] }
>
|
4) SUMMARY>
So in this mongoDB tutorial we learned how to Insert/ Put list of values in array in existing record in MongoDB.
Let’s puts list of items in array in existing document >
> db.testCol.update( {"id":"1"}, {"$pushAll" : {arr1 : [1, 2, 3]}} )
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
What is MongoDB - A quick introduction to database
Update documents in collection in mongoDB
Create new database in mongoDB
Create new collection(table) in mongoDB
Data Modelling in MongoDB - Multiple tables
Labels:
MongoDB