Update field within array - HELP

H, I have a document with an array, here is the structure:

{
_id:"...",
_artist:"...",
_datecreated:"...",
_gallery: [0] {
	 _title: title,
	_description: description
	_datecreated: currentDate,
	_client: client,
	_path: displaypath,
	_artworkid: artworkid,			
},
_shop:"...",
_devdate:"...",
_dev:"...",
_username:"...",
_password:"...",
 }

And I’ve got only one entry in the array, which is 0…

I have succeeded in updating the “_title” field within array using:
"DB.updateOne({"_gallery._artworkid": artid}, {$set: { "_gallery.0._title": newtitle }});"

But I have a problem, thats using a prefixed “.0.” in the function, when really, I want to use that ‘field’ as a variable as in ‘i’ (depending on which ‘i’ is being chosen, the array entry will be updated accordingly), and I have tried all sorts, with no luck to simply include a variable into the function “field” option… Can someone shine a light on this?

This example bellow illustrates what I mean with the ‘i’, there is the same: “array”.“entrynr”.“field”…

I got errors of this sort:

  • "SyntaxError: Unexpected token ‘+’ "
  • MongoServerError: Modifiers operate on fields but we found type string instead. For example: {$mod: {: …}} not {$set: “_gallery.0._title: B Again”}

a bunch more, but this was as close as I got to it…

How can I do this?

HELP x)

Hi :wave: @Illimited_Co,

Welcome to the MongoDB Community forums :sparkles:

I would suggest you use the $arrayFilter and $[<id>] operators.

"DB.updateOne({"_gallery._artworkid": artid}, {$set: { "_gallery.0._title": newtitle }});"

I am going to use a small example to illustrate this. Let’s say you have a collection name “student” that looks like this (resembling yours).

{
   "_id": ObjectId("60103cc232ec3d6175e19185"),
   "name": "John Doe",
   "grades": [
      { "subject": "Maths", "score": 85 },
      { "subject": "English", "score": 92 },
      { "subject": "Science", "score": 78 }
   ]
}

To update the “score” field for Maths subject for a student, we can use the following query:

db.students.updateOne(
   { "_id": ObjectId("60103cc232ec3d6175e19185") },   // Filter by student ID
   { 
      $set: { 
         "grades.$[elem].score": 90 
      }
   },
   {
      arrayFilters: [
         { "elem.subject": "Maths" }   // Filter the "grades" array to find the element with "subject" equal to "Math"
      ]
   }
)

It will return the following output: :point_down:

{
   "_id": ObjectId("60103cc232ec3d6175e19185"),
   "name": "John Doe",
   "grades": [
      { "subject": "Math", "score": 90 },
      { "subject": "English", "score": 92 },
      { "subject": "Science", "score": 78 }
   ]
}

You can similarly approach the above question to update the field matching the specific object in your array of _gallery. Here is the query for your reference:

db.coll.updateOne(
   { "_gallery._artworkid": artid },  
   { 
      $set: { 
         "_gallery.$[elem]._title": newtitle   
      }
   },
   {
      arrayFilters: [
         { "elem._artworkid": artid }   // Filter the "gallery" array to find the element with "_artworkid" equal to "artid"
      ]
   }
)

Let us know if you have any further questions.

Thanks,
Kushagra