Updating an item value which is in an array of objects containing in a document which is in a group of documents

I have seen questions here which may look similar to this but they are not…

I have a list of documents in my database and each document has an array of objects. I want to update a certain value of a certain object in the array but I have no possible solution at the moment…

The solutions am seeing here take into assumption that the document has already been found but I want to first find the right document, then access its array, the access the right object and then finally update the object…

I’ll be glad if I get a response as soon as possible

Good afternoon, welcome to the community.

Can you give an example of your document and which field you are searching for and which field you want to update?

1 Like

Hello, thank you for taking time to reach out. …
I’ve got a sample collection containing 3 documents… supposing I want to update the a value in the links array (the value is inside an object embedded in an array of objects)…
You can use any document for your explanation

[{
“_id”: {
“$oid”: “64ee4ae8535221d4e56a0150”
},
“UserName”: “iamodreck”,
“Email”: “Kigo”,
“PhoneNumber”: “+2”,
“Password”: “”,
“Reg_Date”: “Tue Aug 29 2023 22:45:44 GMT+0300 (East Africa Time)”,
“UserNumber”: 1,
“Links”: [{“A”:1 , “B”:2} , {“A”:1 , “B”:2}]
},
{
“_id”: {
“$oid”: “64fb588b3c896e63ec553b0f”
},
“UserName”: “iam”,
“Email”: “Kiom”,
“PhoneNumber”: “+2”,
“Password”: “Is”,
“Reg_Date”: “Tue Aug 29 2023 22:45:44 GMT+0300 (East Africa Time)”,
“UserNumber”: 1,
“Links”: [{“A”:9 , “B”:2} , {“A”:5 , “B”:2}]
},
{
“_id”: {
“$oid”: “64fb589f3c896e63ec553b10”
},
“UserName”: “ieck”,
“Email”: “Kigom”,
“PhoneNumber”: “776421”,
“Password”: “Isa”,
“Reg_Date”: “Tue Aug 29 2023 22:45:44 GMT+0300 (East Africa Time)”,
“UserNumber”: 1,
“Links”: [{“A”:6 , “B”:2} , {“A”:7, “B”:2}]
}]

If I understood correctly, here is an example of what would solve:

db.cool.updateMany(
  { "events.eventType": "SaleCredit" },
  { $set: { "events.$[elem].eventType": "Sale" } },
  { arrayFilters: [{ "elem.eventType": "SaleCredit" }] }
)

You search the document by filter and only update the array fields that match your filter in arrayFilters. See if it makes sense. I’m available.

the parameters you’ve used have made it more confusing because i don’t have anything like “sales credit” in my documents"

Sorry for confusing you, I’ll put it for your example, follow below:

db.cool.updateMany(
  { "Username": "ieck" },
  { $set: { "Links.$[elem].A": "10" } },
  { arrayFilters: [{ "elem.A": "6" }] }
)

Where the user is ieck and your Links..A is 6, put 10.

is any number in the array.

Thanx… though am just wondering if I can implement this using node js because I’m building a node js service

Yes, you can.

const result = await coll.updateMany(filter, update, options);

These variables are the values, for example:

const options = {[ arrayFilters: … ]}

Thanx so much…
I appreciate this help

1 Like

Hello …hope your doing fine.
If I may ask more about this incident,
In the previous incident, I aimed to update a specific item in an object contained in an array of a specific user . …

But now I have a counter item in each object contained in an array in every document.
And I want to reset the counter to zero but I want this action to be performed on all documents at once.
Could you be having an idea on how I can archive that?

I believe that if you want to update them all, you could put the arrayFilters as $exists: true and the set however you want, wouldn’t that work?

I get the idea. But my biggest problem here is how to organize the syntax.
That’s a big challenge to me so far and I think it’s because am a newbie to the database

Ok, no problem.

Test this:

db.cool.updateMany(
  { "Username": "ieck" },
  { $set: { "Links.$[elem].A": "10" } },
  { arrayFilters: [{ "elem.counter": {$exists: true} }] }
)

Am seeing in your syntax statement, you’ve specified the username ( which I think will only update one document) but in my case , I want to update all documents

Oh yes, true.

db.cool.updateMany(

  {},
  { $set: { "Links.$[elem].A": "10" } },
  { arrayFilters: [{ "elem.counter": {$exists: true} }] }
)

Thank you for this…
It worked

1 Like

I’m glad you got it resolved, I’m at your disposal.

Hello Samuel,
Hope yo doing fine .
I need some help on something, If possible

*Incase I have a number of documents and each of those documents has a field containing email.
How do I check for the existence of a certain email in all the documents without having to return the document but only the response which I think it May either be true (if the email exists) and false if it doesn’t exist

Bom dia! Você quer saber se esse e-mail existe em algum documento, isso? Tente usar uma projeção para retornar o que você precisa.

db.coll.find({},{exists: "true", _id: 0}).limit(1)
[ { existe: 'verdadeiro' } ]

If the solution helped you, put the item as solved, this way it helps other people with the same question ;D

Ive just seen this now but I’ll try it tomorrow in the morning since it’s now late…
About the projection, it failed to work in my code and am still trying to find out why