How do I update an array of strings within an array of objects using ObjectId in mongodb?

Benny

I am having trouble to understand how to update tags array within an object of arrays in mongoDB.

I have an array of objects and its corresponding ObjectID of which tags I would like to update on mongoDB. What I tried is to use a loop using updateOne specifying the ObjectID as a query to find the respective Document.

Something like this:

nodes.forEach(node => {
   const query = { '_id' : ObjectId(node.Id) }

   db.collection(collection).updateOne(
      query,
      { $set: { 'tags': value } },
      function(err, result) {
        if (err) {
            throw err;
        }
        console.log(result);
     });
  });

This did not work unfortunately. I have tried other approaches but I could not find anything that works

I have the following mongodb data structure:

{
"_id": {
  "$oid": "6022bc9f9b55276bd39f4e0"
},
"name": "TestProject",
"projectData": {
  "nodes": [
    {
      "data": {
        "_id": {
          "$oid": "6022bc959b559276bd39f4be"
        },
        "nodeData": {
          "tags": []
        }
      }
    },
    {
      "data": {
        "_id": {
          "$oid": "7022bc959b559276bd39f4ce"
        },
        "nodeData": {
          "tags": []
        }
      }
    },
    {
      "data": {
        "_id": {
          "$oid": "8022bc959b559276bd39f4de"
        },
        "nodeData": {
          "tags": []
        }
      }
    }
  ....
  ]
}

I would like to update only specific tags of objects which match the ones that I pass in as an ObjectID. What would be the best approach to update a bunch of object values matching a specific ObjectID?

turivishal

You can use, the filtered positional operator $[<identifier>] identifies the array elements that match the arrayFilters conditions for an update operation.

  • If you are updating same tags in each node id than try $in condition other wise you can use bulkWrite() operation.
let value = ["tag1", "tag2"];
let nodes = [
  ObjectId("6022bc959b559276bd39f4be"),
  ObjectId("7022bc959b559276bd39f4ce")
];

db.collection(collection).updateOne(
  {},
  { $set: { "projectData.nodes.$[d].data.nodeData.tags": value } },
  {
    arrayFilters: [
      { "d.data._id": { $in: nodes } }
    ]
  }
)

Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update mongodb array of ObjectId to array of objects?

How do i add/update ObjectId array in a collection using MongoDB(Mongoose)?

Update an array of strings nested within an array of objects in mongodb

How can I update the state of objects within an array using React?

How do I update all objects in an array in mongodb

How do I update an object within an array using findIndex?

Remove from array of objectId using update mongoDB

How Do I Update a Property in Array of Objects using Redux Reducers?

NodeJS and MongoDB - Array of ObjectId how to return Objects

Mongoose - How to update all objects within an array in MongoDB?

How do I update an array within an object from within state?

How to update array of objects in mongodb?

How can I unset a value within a nested array of objects? MongoDB

How do I map array of objects to an existing array on MongoDB

Update objects within an array

How do I append a string to an array of strings within a loop in C?

How do I search for a particular object within an array of JSON objects using postman

How can I access the "name" and "ObjectId" from this array of Objects? (Mongoose, MongoDB)

How to update multiple objects in array in a single mongodb document using mongoose

How do I update an array using useState?

How do I $push a value to an array within an embedded document in MongoDB using Mongoose?

How do I update an array within a document using existing values (for concatenation)?

How do I take an array of strings and separate them further into objects?

How do i filter an array inside of a array of objects using filter?

Mongodb update array of objects

How do I print out an class of array objects that has multiple scores within those objects using the arrow pointer?

MongoDB Query Array of Objects within Array of Objects

How do I get all matching sub array Objects in mongoDB

How do I find the intersection between an array of objects and a collection in MongoDB?