Search code examples
c#arraysmongodbsortingmongodb-.net-driver

Sort Specification of Non-Document nested Array during a $push operation


Given a document structure:

[{
    _id: ObjectId("someId"),
    Property: {
        CollectionProperty: [{
            ItemKey: 28,
            StringArray: [
                "ItemA",
                "ItemB"
            ]
        }]
    }
}]

Performing the following operation in the mongo shell does what I want:

db.getCollection('collection').updateOne(
{ 
    "_id" : ObjectId("someId"), 
    "Property.CollectionProperty.ItemKey" : 28 
}, 
{ $push : 
    { 
        "Property.CollectionProperty.$.StringArray" : { 
            $each : [ "AItemA" ], 
            $sort : 1
        }
     } 
});

However, I cannot find in the documentation or anywhere on the net how to reproduce THAT $sort command in C# code. I can reproduce if it had fields by using "field":1, but not a non-document array like that.

The mongodb documentation that describes that sort is here: MongoDB Sort Elements that are not documents

Here's what I have in C# so far:

Builders<DataType>.Update.PushEach("Property.CollectionProperty.$.StringArray",
                new[] { "AItemA" }, null, null, ???)));

The ??? is what I don't know how to do... does anyone have any suggestions?

TIA


Solution

  • This is apparently currently unsupported, but planned for a future release in the C# driver, as documented here:

    https://jira.mongodb.org/browse/CSHARP-1271