How to Update different fields in same array having unique fields.fieldname as shown below ! Please need help

///////* the input *///////

{
“_id” : “lpu_website_students”,
“fields” : [
{
“fieldname” : “student_id”,
“type” : “abhi”,
“searchable” : true,
“required” : true,
“facetable” : false,
“is_id” : true,
“typ” : “string”,
“t” : “kunall”
},
{
“fieldname” : “name”,
“type” : 12.0,
“searchable” : true,
“required” : true,
“facetable” : false,
“is_id” : false
}
]
}

//////*output//////

{
“_id” : “lpu_website_students”,
“fields” : [
{
“fieldname” : “student_id”,
“type” : “jugunu”, //changed
“searchable” : yes, //changed
“required” : true,
“facetable” : false,
“is_id” : true,
“typ” : “string”,
“t” : “kunall”
},
{
“fieldname” : “name”,
“type” : 12.0,
“searchable” : nope, //changed
“required” : true,
“facetable” : false,
“is_id” : present //changed
}
]
}

Hey @Kunal_51024

Could you help me understand what you are trying to do a bit more. The title says you want to update different fields in a different array but your example looks like you are modifying two object in the same array, which have different fields.

To make things more clear could you please make use of backtick character at the top left of the keyboard for code formatting.

const input = {
  _id: lpu_website_students,
  fields: [
    {
      fieldname: student_id,
      type: abhi,
      searchable: true,
      required: true,
      facetable: false,
      is_id: true,
      typ: string,
      t: kunall,
    },
    {
      fieldname: name,
      type: 12.0,
      searchable: true,
      required: true,
      facetable: false,
      is_id: false,
    },
  ],
};

const output = {
  _id: lpu_website_students,
  fields: [
    {
      fieldname: student_id,
      type: jugunu, //changed
      searchable: yes, //changed
      required: true,
      facetable: false,
      is_id: true,
      typ: string,
      t: kunall,
    },
    {
      fieldname: name,
      type: 12.0,
      searchable: nope, //changed
      required: true,
      facetable: false,
      is_id: present, //changed
    },
  ],
};

And have you looked at the docs to help as well.
https://docs.mongodb.com/manual/reference/operator/update-array/#array-update-operators
https://docs.mongodb.com/manual/reference/operator/aggregation/map/

Hello Sir,
Sorry i miss-typed the topic.!! its “How to modify multiple objects in the same array, which have different fields as show in json it is having unique fields.fieldname”.

Could you use $map and then $cond to figure out what object in the array you are currently examining with $map then update the fields accordinly.

This all depends on the structure of the array of object. Is it known or are they ‘random’.

Say there was an array of people objects, and I wanted to update each person differently depending on what gender they are. My above logic would work. However, if it is an array of products for Amazon(vastly different fields) then you may need a more complex solution.

hello sir,
the example which you gave about the array of people objects is similar.I want to update each person differently depending on what gender they are

Ok, so using a brand new feature in MongoDB 4.2 you can use the new aggregation pipeline in update() operations.

Starting in MongoDB 4.2, an aggregation pipeline. The pipeline can consist of the following stages:

Therefore something like the below could work.

db.people.update(
  { _id: lpu_website_students }, // query condition
  [ // pipeline update argument
    {
      $set: { // $set is just like $addFields
        fields: { // therefore set fields field to
          $map: { // a map of
            input: "$fields", // the original fields
            as: "field", // as $$field
            in: { $cond: { // to outcome is based off a condition if
              if: { $eq: [student_id, "$$field.fieldname"]}, // the $$field.fieldname equals student_id
              then: { $mergeObjects: ["$$field", { /* NEW FIELD VALUES */ }] },
              else: { $mergeObjects: ["$$field", { /* NEW FIELD VALUES */ }] },
            }}
          }
        }
      }
    }
  ]
);

Keep in mind this required v4.2 which is not available on the M0, M2, or M5 Atlas tiers. Which is a HUGE disappointment!! As I am a MongoDB Atlas user who is upset about this. :disappointed:

Sir,
I not able to understand the “$$field” part can you please use the same example which i have used and changed some fields in it

$$field is a reference to each of the objects inside your fields array. This is just the way the $map aggregation expression labels each item, with the as keyword.

So each object in your fields array will become the $$field as $map walks the array.

Ill edit my post to be more inline with your example.

Sir but this is not my solution ! But thanks for your help :+1: