0

I'am currently working on our persisters to update an answer option, I have the following code:

public async Task Update(ItemList list, Guid id, Option option, CancellationToken cancellationToken = default)
{
    var filter = Builders<ItemList>.Filter.And(
        Builders<ItemList>.Filter.Eq("_id", list.Id),
        Builders<ItemList>.Filter.Eq("Items._id", id)
    );
    var update = Builders<ItemList>.Update.Set("Items.$[i].Options.$[j]", option);
    
    var arrayFilters = new List<ArrayFilterDefinition<ItemList>>
    {
        new BsonDocumentArrayFilterDefinition<ItemList>(
            new BsonDocument("i._id", elementId)
        ),
        new BsonDocumentArrayFilterDefinition<ItemList>(
            new BsonDocument("j._id", option.Id)
        )
    };      
    
    var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
    
    await _collection.UpdateOneAsync(filter, update, updateOptions, cancellationToken);        
}

In the database we store ids are guids structure looks like this:

{
  "_id": "ae39e133-18a6-43b0-8745-b13a00dd8632",
  “ItemId”: "af21eeb1-e952-4e10-af59-b13800cf8880",
  “Items”: [
    {
      "_t": “Item”,
      "_id": "e42f593e-1444-4f3a-9991-b15d008d9a99",
      "Options": [
        {
          "_id": "7dcccc2e-949a-4743-bd53-b15d008d9aa5",
          "Label": “test”,
          "Value": "a4e1c768-6cf9-417d-82bd-275c92be6a91",
          }
        }
      ]
    }
  ]
}

Putting the query in 3t studio it gives me MongoInvalidArgumentError: Update document requires atomic operators, this is a logged query that is being executed, c# driver does not give any error.

db.getCollection("Defintion").updateOne(
{ "update" : "Defintion", "ordered" : true, "$db" : "dbName", "lsid" : 
    { "id" : CSUUID("28fd5c50-4d0b-4a71-a2d4-d6c598b6f413") }, 
    "updates" : [{ "q" : { "_id" : "ae39e133-18a6-43b0-8745-b13a00dd8632", "Items._id" : "e42f593e-1444-4f3a-9991-b15d008d9a99" },
         "u" : { "$set" : { "Items.$[i].Options.$[j]" : 
             { "_id" : "7dcccc2e-949a-4743-bd53-b15d008d9aa5", "Label" : "Test2", "Value" : "a4e1c768-6cf9-417d-82bd-275c92be6a91"} } }, 
                 "arrayFilters" : [{ "i._id" : CSUUID("e42f593e-1444-4f3a-9991-b15d008d9a99") }, { "j._id" : CSUUID("7dcccc2e-949a-4743-bd53-b15d008d9aa5") }] }] }

)
8
  • what exactly you put into 3t studio? Does this error happens in c# driver? If no, what's your issue there? Commented Apr 29, 2024 at 18:14
  • @dododo added the query, C# driver is not giving any error at all Commented Apr 30, 2024 at 6:33
  • it's incorrect, the update root level will be added by updateOne method Commented Apr 30, 2024 at 6:41
  • options - should be provided by options class Commented Apr 30, 2024 at 6:42
  • $db - is a technical field, so all of this is just incorrect Commented Apr 30, 2024 at 6:42

1 Answer 1

0
+50

in studio 3T for given sample you can use below update command:

db.getCollection("Defintion").updateOne(
    { "_id" : "ae39e133-18a6-43b0-8745-b13a00dd8632", "Items._id" : "e42f593e-1444-4f3a-9991-b15d008d9a99" },
     { "$set" : { "Items.$[i].Options.$[j]" : { "_id" : "7dcccc2e-949a-4743-bd53-b15d008d9aa5", "Label" : "Test2", "Value" : "a4e1c768-6cf9-417d-82bd-275c92be6a91"} }},
    {  "arrayFilters" : [{ "i._id" : "e42f593e-1444-4f3a-9991-b15d008d9a99" }, { "j._id" : "7dcccc2e-949a-4743-bd53-b15d008d9aa5" }]}
    
    )

and syntax for update is like below from documentation:

db.collection.updateOne(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>,
     let: <document>
   }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.