C# MongoDB - How to add and remove item to multiple nested array elements?

我的未来我决定 提交于 2019-12-24 12:06:19

问题


I have a document in MongoDB that looks like that:

{
    "Id":"123",
    "Product": "test",
    "Tags":[
        {
            "Name": "name",
            "Categories": [
                {
                    //item
                },
                {
                    //item
                }
            ]
        },
        {
            "Name": "name",
            "Categories": [
                {
                    //item
                },
                {
                    //item
                }
            ]
        }
    ]
}

Now, I need to add a new Item and it needs to be added to all of the categories of the Tags element of that Product by its Id. For example, when I'll insert Item 3 the document should look like this:

{
    "Id":"123",
    "Product": "test",
    "Tags":[
        {
            "Name": "name",
            "Categories": [
                {
                    //item 1
                },
                {
                    //item 2
                },
                {
                    //item 3
                }
            ]
        },
        {
            "Name": "name",
            "Categories": [
                {
                    //item 1
                },
                {
                    //item 2
                },
                {
                    //item 3
                }
            ]
        }
    ]
}

and same goes for removing an item, it needs to be removed from all of the categories as well. Is there a way to do that with the C# MongoDB Driver without pulling the object and "manually" updating them?


回答1:


You can try something like below in 2.5 driver with 3.6 version.

Finds the document with filter criteria and update which includes new positional identifier to update multiple elements in array inside UpdateOne method.

$[] updates all the Tags arrays to include new item in all Categories array. It acts as a placeholder for updating all elements in array.

Push

var filter = Builders<Product>.Filter.Eq("Id", "123");
var update = Builders<Product>.Update.Push("Tags.$[].Categories", "Item 3");
var result = collection.UpdateOne(filter, update);

Pull

var filter = Builders<Product>.Filter.Eq("Id", "123");
var update = Builders<Product>.Update.Pull("Tags.$[].Categories", "Item 3");
var result = collection.UpdateOne(filter, update);

Additional Information:

You can set the ArrayFilters options in UpdateOptions for applying query criteria on nested array to control what elements to update.

For example to update all the Categories in Tags array where each tag has Name name.

var filter = Builders<Product>.Filter.Eq("Id", "123");
var update = Builders<Product>.Update.Push("Tags.$[t].Categories", "Item 3");
var arrayFilters = new List<ArrayFilterDefinition>{ new ArrayFilterDefinition(new BsonDocument("t.Name", "name")) };
var updateOptions = new UpdateOptions({ArrayFilters = arrayFilters});
var result = collection.UpdateOne(filter, update, updateOptions);


来源:https://stackoverflow.com/questions/48009922/c-sharp-mongodb-how-to-add-and-remove-item-to-multiple-nested-array-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!