Push an object into an array, if the array exists, otherwise create an array with the object in MongoDB

I have a collection with the following document:

{ _id : "abcd", name : "Tom", myArray : [ { field1 : "", field2 : "" } ] }, { _id : "efgh", name : "Jerry" } 

I have a new object for myArray . I want to write a request to update only one document.

If the query matches the document using _id : "abcd" , then click the new object in the myArray field:

 { _id : "abcd", name : "Tom", myArray : [ { field1 : "", field2 : "" }, { // new object } ] } 

And if the request matches _id : "efgh" , create a myArray field with a new object inside it:

 { _id : "efgh", name : "Jerry" myArray : [ { // new object } ] } 

How can i achieve this?

+8
mongodb mongodb-query
source share
2 answers

To explain all the possible cases here, consider each case of the document:

If your document looks like this:

 { "_id": "efgh", "name": "Jerry" } 

Then update this status:

 db.collection.update( { "_id": "efgh" }, { "$push": { "myArray": { "field1": "abc", "field2": "def" } } ) 

Results in this:

 { "_id": "efgh", "name": "Jerry", "myArray": [ { "field1": "abc", "field2": "def" } ] } 

So, the array is created and a new element is added.

If your document already has such an array:

 { "_id": "abcd", "name": "Tom", "myArray": [ { "field1": "", "field2": "" } ] } 

And you basically make the same statement:

 db.collection.update( { "_id": "abcd" }, { "$push": { "myArray": { "field1": "abc", "field2": "def" } } ) 

Then the contents of the new document are added to the existing array:

 { "_id": "abcd", "name": "Tom", "myArray": [ { "field1": "", "field2": "" }, { "field1": "abc", "field2": "def" } ] } 

If your source document has a named field, but it is not an array, for example:

 { "_id": "efgh", "name": "Jerry", "myArray": 123 } 

Then make sure that it is not an array by testing under the conditions of the query and instead of $set :

 db.collection.update( { "_id": "efgh", "myArray.0": { "$exists": false } }, { "$set": { "myArray": [{ "field1": "abc", "field2": "def" }] } ) 

This will safely overwrite an element that is not an array (the dotted notation "myArray.0" means the first element of the array, which is incorrect) with a new array containing your content. The result is the same as the original:

 { "_id": "efgh", "name": "Jerry", "myArray": [ { "field1": "abc", "field2": "def" } ] } 
+15
source share

$ Push is what you need to achieve this. If myArray already exists, then $ push will insert newObject into it (similar to the STACK data structure operation). If myArray does not exist, it will create the "myArray" field as a key and insert a new object into it (like $ Set oprtaion)

Consider the following sample document:

 { _id : "abcd" myArray:[ { field1:"mango", field2:"apple" } ] } 

now insert the following object into it:

 var newObject = {field1:"example1" , field2:"exmaple2"}; 

Request:

 db.collection.update({_id:"abcd"},{"$push":{myArray: newObject }}); 

after executing the above request, the result is as follows:

 { _id : "abcd" myArray:[ { field1:"mango", field2:"apple" }, { field1:"example1" , field2:"exmaple2" } ] } 

Now consider another example in which the key "myArray" DOES NOT ALLOW in the document:

 { _id:"efg" } 

so paste the object below:

  var newObject2 = { field1 : "exp2" , field2 : "exp1" } 

Request:

  db.collection.update({_id:"efg"},{"$push":{myArray: newObject2 }}); 

The result of the above UPDATE operation is below:

  { _id : "efg" myArray : [ { field1 : "exp2" , field2 : "exp1" } ] } 
+2
source share

All Articles