Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested array $pull query using C# MongoDB driver

Tags:

c#

shell

mongodb

I have this following query working on mongo shell as expected.

db.getCollection('personnels').update(
    {
        _id: ObjectId("55f6728b9d73a15807885de8"), 
        "Devices._id":ObjectId("55fa5f7ac9e7863a3836e331")
    }, 
    {
        $pull:{ "Devices.$.DeviceCloudFolders": { "CloudFolderId": ObjectId("5615124b06275f072040c4f1")}}
    }
);

And here is my document structure:

{
    "_id" : ObjectId("55f6728b9d73a15807885de8"),
    "FirstName" : "Tolga",
    "Devices" : [ 
        {
            "_id" : ObjectId("55fa5f7ac9e7863a3836e331"),
            "Name" : "tolga-laptop",
            "DeviceCloudFolders" : [{
                "AuthorityType" : 1,
                "CloudFolderId" : ObjectId("55f96db5c9e7863a3836e310"),
                "Status" : 1
            }],
            "Status" : 1
        }
    ],
    "Status" : 1
}

I need to use it in C# and couldn't figure out how.

I started with these lines:

var filter = Builders<Personnel>.Filter.And(
                 Builders<Personnel>.Filter.Eq("_id", ownerPersonnelId),
                 Builders<Personnel>.Filter.Eq("Devices._id", _id));

var update = Builders<Personnel>.Update.PullFilter("Devices.$.DeviceCloudFolders", /*couldn't figure out what goes here*/))

Personnels.FindOneAndUpdateAsync(filter, update);
like image 267
Tolga Evcimen Avatar asked Oct 07 '15 15:10

Tolga Evcimen


1 Answers

I'm not sure, but you can try using this:

var update = Builders<Personnel>.Update.PullFilter(
    "Devices.$.DeviceCloudFolders", 
    Builders<DeviceCloudFolder>.Filter.Eq("CloudFolderId", _cloudFolderId));
like image 86
offi Avatar answered Oct 16 '22 23:10

offi