[SERVER-49812] How to upsert an element in mongodb array? Created: 22/Jul/20  Updated: 24/Jul/20  Resolved: 24/Jul/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Shubham Singh Assignee: Jonathan Streets (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Suppose our document looks like this

{
   a:1,
   b:[
    {c:120,d:100},
    {c:121,d:110}
   ]
}

Now how could I upsert new objects in this array? Suppose I want to perform update on the above document and add {c:200,d:120} to b so my expected result looks like this

{
   a:1,
   b:[
    {c:120,d:100},
    {c:121,d:110},
    {c:200,d:120}
   ]
}

Also the update will of $inc, meaning suppose I want to increment d by 200 if c is present(lets say c is 200 and it is already present in the above document), if not present then I want to upsert the document itself.



 Comments   
Comment by Jonathan Streets (Inactive) [ 23/Jul/20 ]

Hi Shubham Singh,

I now see that the requested functionality is out of the scope of this project. Please note that the SERVER project is for bugs and feature suggestions for the MongoDB server. As this ticket does not appear to be a bug, I will now close it. If you need further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Community Forums or on Stack Overflow with the mongodb tag.

Kind regards,
Jon

Comment by Shubham Singh [ 23/Jul/20 ]

Same question again, how does it help me with the the increments if the document is already present, because that is also part of the desired functionality.

Comment by Jonathan Streets (Inactive) [ 22/Jul/20 ]

the document will only be updated if the array does not contain b.c:200.

Comment by Shubham Singh [ 22/Jul/20 ]

and how will the updates(increments) be handled by the above mentioned query, which happens in case the value of c is present?

Comment by Jonathan Streets (Inactive) [ 22/Jul/20 ]

i googled mongodb add to array if not exists and found https://stackoverflow.com/questions/38970835/mongodb-add-element-to-array-if-not-exists

this command answers the next question:

db.mydb.update( { a:1, "b.c": {$ne: 200}}, {$push: { b: {c:200, d:120}}})

Comment by Shubham Singh [ 22/Jul/20 ]

Not exactly. 

I want to update the objects in the array using one of the properties. e.g. in the case of above document example, I want to update the data in array b on the basis of property c, if given value of c is not present, new object should be pushed to array b (i.e., an insert should happen as a result of upsert command)

Comment by Jonathan Streets (Inactive) [ 22/Jul/20 ]

i googled "mongodb add to array" and found this -

https://docs.mongodb.com/manual/reference/operator/update/push/

is this what you are looking for?

db.mydb.update({a:1},{$push: {b:{c:200,d:120}}})

gave me

> db.mydb.find().pretty()
{
	"_id" : ObjectId("5f1894ab129a41cdd58eccb5"),
	"a" : 1,
	"b" : [
		{
			"c" : 120,
			"d" : 100
		},
		{
			"c" : 121,
			"d" : 110
		},
		{
			"c" : 200,
			"d" : 120
		}
	]
}

Comment by Shubham Singh [ 22/Jul/20 ]

I tried using something similar to 

db.<collectionName>.update(
    {},
    {$inc:{"b.$[element].d":20}},
    {arrayFilters:[{"element.c":2}],upsert:true}
)

But upsert doesn't seem to work. (values are arbitrary and are not related to example above in any manner)

Generated at Thu Feb 08 05:20:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.