18 Jan 2014

Using $push, $pop, $pull, $pushAll, $pullAll & $addToSet operators in MongoDB

1/18/2014

As documents can have arrays inside it and in more practical scenarios you will realize in most of the practical scenarios you would use arrays inside documents that you store and update in a MongoDB.

However using normal update method from MongoDB you can update elements or values inside a document.

Let't take an example, say i have a document in a collection called testarray
{ "_id" : "1", "test" : [  1,  2,  3,  4 ] }

Now say i want to update the 2nd element in the array and set the value as 5 instead of 2, i will do something like
> db.testarray.update({"_id":"1"},{$set:{"test.1":5}})

which would result and set the document now to be
{ "_id" : "1", "test" : [  1,  5,  3,  4 ] }

Now say we need to add another element at the end of the document, one way is
>  db.testarray.update({"_id":"1"},{$set:{"test.4":5}})

Now via this statement i am asking the document to be updated and value 5 to be set at 5th location in the array, the resulting document would be
{ "_id" : "1", "test" : [  1,  5,  3,  4 ,5] }

Now we can think of and do a lot of manipulation here but we first need to be aware what is the length of the array and in case of deleting something, we can set the value at the position as null but not squeeze the size of the array.

To deal with such scenarios and to deal with array manipulation in a document more efficiently, we have  $push, $pop, $pull, $pushAll, $pullAll & $addToSet operators in MongoDB, lets take them one by one

$push

$push operator is an operator in MongoDB that would add a new element in the array on the right.
Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5 ] }

To add a new element to the array i can simply say
> db.testarray.update({"_id":"1"},{$push:{"test":6}})

and now when i do a find on the collection 
> db.testarray.find()
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5,  6 ] }

$pop

$pop unlike $push is an operand in MongoDB to remove elements from the array in a document from the right hand side.

Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5, 6 ] }

To add a new element to the array i can simply say
> db.testarray.update({"_id":"1"},{$pop:{"test":1}})

and now when i do a find on the collection 
> db.testarray.find()
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5] }

The value that you specify against the field in the $pop document is actually irrelevant and does not hold any significance, however a negative value for the same would pop elements from the left and side rather than right.

$pull

We learned above as to remove elements from the right or left of an array using the $pop operator, additionally MongoDB gives you a operand to remove a value from an array irrespective of its position in the array

Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5 ] }

To add a more that one element to the array i can simply say
> db.testarray.update({"_id":"1"},{$pull:{"test":4}})

and now when i do a find on the collection
> db.testarray.find()
{ "_id" : "1", "test" : [  1,  2,  3,  5 ] }

In case there are multiple entries in the array with the same value , the pull operator would remove all the elements in the array matching the value being passed for $pull.

$pushAll

Very similar in terms of functioning as compared to $push, the $pushAll operator adds more than one element in the document

Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5 ] }

To add a more that one element to the array i can simply say
> db.testarray.update({"_id":"1"},{$pushAll:{"test":[6,7,8]}})

and now when i do a find on the collection
> db.testarray.find()
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5,  6,  7,  8 ] }

$pullAll

Very similar in terms of functioning the $pullAll operator removes more than one element in the document matching the values passed in the array while calling the $pullAll

Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5 ,1] }

To remove elements with value 1 and 5 in the array i can simply say
> db.testarray.update({"_id":"1"},{$pullAll:{"test":[1,5]}})

and now when i do a find on the collection
> db.testarray.find()
{ "_id" : "1", "test" : [  2,  3,  4] }

$addToSet

There are situations while data modelling where you sometime want to treat the document as a unique set of elements. To support such requirements MongoDB gives you the $addToSet operator, which by definition adds  a element value in the array towards the right if it does not exist in the array, if it does , the execution of the statement with the operator would have no effect.

Say we have a document as 
{ "_id" : "1", "test" : [  1,  2,  3,  4,  5] }

To remove elements with value 6 in the array i can simply say
> db.testarray.update({"_id":"1"},{$addToSet:{"test":6}})

and now when i do a find on the collection
> db.testarray.find()
{ "_id" : "1", "test" : [  1, 2,  3,  4, 5, 6] }

In case you execute the statement again , it wont have any effect as the element 6 is already there in the array.

I guess that's all, you should by now be comfortable playing around with arrays in your document.



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. How will we perform the first query using array operators

    "Now say i want to update the 2nd element in the array and set the value as 5 instead of 2"

    ReplyDelete

 

© 2013 NimbleGeek. All rights resevered. Designed by Templateism

Back To Top