Updating a mongoDB document is not working

Hi,

I am using java script to construct a propr statement in order to update json stored in MongoDB (Azure CosmosDB).

That is an example of the json document that is stored in the mongo DB:

{
  "_id": {
    "$oid": "XXXXXXXX"
  },
  "relatedOe": {
    "nameOE1": {
      "name": "company1",
      "sevDeskContactId": "XXXXX",
      "userCount": 23,
      "lastUpdate": "2023-10-31 07:20:13",
      "webinarFlatrate": false,
      "createInvoice": true,
      "dateOfActivePayment": "2023-05-30 00:00:00"
    },
    "nameOE2": {
      "name": "company2",
      "sevDeskContactId": "XXXXXXX",
      "userCount": 54,
      "lastUpdate": "2023-10-31 07:20:12",
      "webinarFlatrate": false,
      "createInvoice": true,
      "dateOfActivePayment": "2023-05-30 00:00:00"
    },
    "nameOE3": {
      "name": "company3",
      "sevDeskContactId": "XXXXXX",
      "userCount": 23,
      "lastUpdate": "2023-10-31 07:20:12",
      "webinarFlatrate": false,
      "createInvoice": true,
      "dateOfActivePayment": "2023-10-01 00:00:00",
      "useSEPA": true,
      "sendInvoice": true,
      "setupIntent": "123123123123123123123123"
    }
  },
  "name": "Big Holding",
  "aggregatedInvoice": false,
  "dateOfActivePayment": "2023-05-30 00:00:00",
  "createInvoice": true,
  "priceModel": "iouaosjdlksjdlkjajsd",
  "priceKey": "12314324234",
  "webinarPriceKey": "123123123",
  "aggregatedUserCount": 100,
  "id": 79,
  "dateCreated": "2023-07-16 18:23:18"
}

I have already a working mechanism to update the attributes on the highest hierachy, see here: Whats the best way to save changes (from a table) to a mongoDB?.

But now I am trying to build a query in order to update the relatedOe object/s. The changes to that objects are found in the table2.changearraySet.

So the best way that I could find (using Chat GPT) is in order to update the tealtedOe objects is following:

  • JavaScript to iterade over changes in "table2.changesetArray" and calls a MongoDB query to update the documents
  • Resource query MongoDB to update every single object

Here is the code...

JavaScript snippet:

let changesetArray = table2.changesetArray;

console.log(changesetArray)

changesetArray.forEach(change => {
    let updateFields = {};
    let oeName = change.name; // Assuming 'name' is the key of the relatedOe object
    console.log(change)
    delete change.name; // Remove the name key as it's not part of the update fields

    console.log(change)
    
    for (let key in change) {
        if (change.hasOwnProperty(key)) {
          // Construct the update path for the nested fields
          let fieldPath = `relatedOe.${oeName}.${key}`;
          updateFields[fieldPath] = change[key];
          console.log(fieldPath)
        }
    }

    console.log(updateFields)
    console.log(typeof updateFields)

    // Convert updateFields to a JSON string and then parse it back to an object
    let updateFieldsJson = JSON.stringify(updateFields);
    console.log("Parsed Stuff")
    console.log(updateFieldsJson)
    let parsedUpdateFields = JSON.parse(updateFieldsJson);
    console.log(parsedUpdateFields)
  
    // Trigger the MongoDB update query
    updateRelatedOE.trigger({
      additionalScope: {
        // The main document's _id
        idToUpdate: selectedCompany_ID, 
        updateFields: parsedUpdateFields
        //idToUpdate: "64b7912d8b44dc7f06728ddb", 
        //updateFields: {"relatedOe.lebensmut.webinarFlatrate": false}
      }
    });
});

Mongo DB Query:

I am getting following error from the MongoDB query:

updateRelatedOE failed (0.001s):Failed to execute 'postMessage' on 'Window': function(...t){let n;try{n=e(...t)}catch(e){return}return n instanceof Promise?new Promise((e=>{n.then(e).catch((()=>e()))})):n} could not be cloned.

I tried to replicate the update call with static values and it is working if I am handing over:

    updateRelatedOE.trigger({
      additionalScope: {
        idToUpdate: "64b7912d8b44dc7f06728ddb", 
        updateFields: {"relatedOe.lebensmut.webinarFlatrate": false}
      }
    });

I have no idea why this is not working....I need help here.

Hello, where selectedCompany_ID is defined, or it is a temp state variable?
if it is a temp state variable you need to add .value to get the actual value
image

1 Like

Hi @Oscar_Ortega,

actually that was the error.... thanks you so much :slight_smile:

Now it is working.

But what do you think in gerneral, is that a goof way to update documents in the MongoDB by iterating over the changesetArray and triggering several resource queries?

Maybe you have a better solution for that?

Best regards

Leon

@leonK-DI You can also try a Bulk Write action where you can submit an array of write actions to your collection.

Hi @joeBumbaca could you make a concrete example with data that I provided?