0

I am going deeper in MongoDB with my project and facing complex mql queries where I have to update/remove sub-sub-..-sub document(s). I realized that I don't quite understand how the path selector works in MongoDB, because at every new query which might be slightly different then a previous one, I google it, and now I have mixture of queries build with BsonDocuments and C# MongoDB generic Builders. So there is no consistency in my code and in my understanding.

Here is the list of questions I am trying to understand for myself, and I would really appreciate if you help me with it:

  • In MongoDB C# Driver, when I have to use Pull, PullFilter, PullAll, and when I have to use Unset instead of Pull?
  • How to write Filter in MongoDB C# Driver for multi-level sub documents? If I start with Builders<MyEntity>.Filter, then I cannot use/combine it with some inner sub-sub-type like Builders<MySubSubEntity_BelongsTo_MySubEntityArrayFromMyEntity>.Filter
  • Trying to solve previous problem, I started to use ArrayFilterDefinition, and here is another problem of mql path understanding.

I have created a sample collection, and trying to do exercises:

  • How to write a query to remove all null items in sub1_item_array_subArray1_itemArray
  • How to change value of deepInsiderItem2

Here is my testing collection:

[{
    "document": {
        "sub1": 
        [
            {
                "sub1_item_array1" : [
                    {"sub1_item_array_subArray1": [
                        {"sub1_item_array_subArray1_itemArray":[
                            {"deepInsiderItem1": "value1"},
                            {"deepInsiderItem2": "value2"},
                            null,
                            null
                        ]}
                    ]}
                ],
                "sub1_item" : 
                [
                    {"sub1_value1_subItem" : "sub1_value1_subItem_value"},
                    {"sub1_value2_subItem" : "sub1_value2_subItem_value"},
                    null,
                    null
                ],
                "sub2_item" : null,
                "sub3_item" : null,
                "sub4_item" : 
                [
                    null,
                    null
                ],
            },
            null,
            null,
        ]
    }
}]

Thank you in advance.

The main problem, when I google for the answers, some of them are using BsonDocument to constract a query, some of them generic Buidlers<>, and I still cannot figure out what would be the best practices.

Dmitry
  • 661
  • 5
  • 21

0 Answers0