使用C#MongoDB驱动程序的嵌套数组$ pull查询 [英] Nested array $pull query using C# MongoDB driver

查看:46
本文介绍了使用C#MongoDB驱动程序的嵌套数组$ pull查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下查询在mongo shell上正常工作.

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")}}
    }
);

这是我的文档结构:

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

我需要在C#中使用它,但不知道如何使用.

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

我从以下几行开始:

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);

推荐答案

我不确定,但是您可以尝试使用此方法:

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

var update = Builders<Personnel>.Update.PullFilter(
    "Devices.$.DeviceCloudFolders", 
    Builders<DeviceCloudFolder>.Filter.Eq("CloudFolderId", _cloudFolderId));

这篇关于使用C#MongoDB驱动程序的嵌套数组$ pull查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆