3

I have a MongoDB Document as follows

{
"_id" : ObjectId("5a55775cbd12982cc063c71a"),
"ShipmentNumber" : "00004000000048652254",
"Cartons" : [ 
    {
        "_id" : ObjectId("5a5575bcbd12982cc063b718"),
        "CartonNumber" : "0076013926580S",
        "Skus" : [ 
            {
                "_id" : ObjectId("5a5575bcbd12982cc063b719"),
                "SkuNumber" : "06577647",
                "ShippedQuantity" : 12,
            },
            {
                "_id" : ObjectId("5a5575bcbd12982cc063b519"),
                "SkuNumber" : "06577657",
                "ShippedQuantity" : 15,
            }
        ],
        "IsScanned" : false,
    },
}

How can I update the "ShippedQuantity" for a particular Sku element based on its "_id" in C# code ?

I tried something like below. But it is not working. Getting error message like

cannot use the part (Cartons of Cartons.$[].Skus.$.ShippedQuantity) to traverse the element

var filter = Builders<BsonDocument>.Filter.Eq("Cartons.Skus._id", new ObjectId("5a5575bcbd12982cc063b519"));
var update = Builders<BsonDocument>.Update.Set("Cartons.$[].Skus.$.ShippedQuantity", 50)

I am facing difficulties when I try to update multi level documents. (In this case I have a list of Cartons and each carton will have its own list of skus and I need to update a specific sku's element)

Please provide a solution or alternative approach to update this inner level (more than 2 levels) documents in MongoDB using C#.

I updated my MongoDB server to the latest 3.6.1. But that is also not helping.

Thanks for your help.

1 Answer 1

11

First, you need to run this command in your MongoDB to apply the new features of version 3.6.1 db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

Here is the code you need for that update:

var filter = Builders<YOUR_CLASS>.Filter.Eq("_id", new ObjectId("5a55775cbd12982cc063c71a"));
var update = Builders<YOUR_CLASS>.Update.Set("Cartons.$[i].Skus.$[j].ShippedQuantity", 50);

var arrayFilters = new List<ArrayFilterDefinition>
{
    new BsonDocumentArrayFilterDefinition<Setup>(new BsonDocument("i._id", new ObjectId("5a5575bcbd12982cc063b718"))),
    new BsonDocumentArrayFilterDefinition<Setup>(new BsonDocument("j._ID", new ObjectId("5a5575bcbd12982cc063b719")))
};
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
var (updated, errorMessage) = await UpdateOneAsync(filter, update, updateOptions);

Additionally, you can run set these settings in your MongoDB to look at your final queries and run them manually in RoboMongo or Studio 3T to debug them:

db.setProfilingLevel(2)   -> to view query logs under C:\data\log\mongod.log
db.setLogLevel(5)         -> to view query logs under C:\data\log\mongod.log

look for the "UPDATE" query in the log file. After that, you can reset the log setting back to 0

db.setProfilingLevel(0)
db.setLogLevel(0)

I've had the same problem and asked the same question Here Have a look at it.

Sign up to request clarification or add additional context in comments.

1 Comment

Can we do this using mongo .net fluent library?

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.