简体   繁体   English

使用Mongo C#2.0驱动程序替换嵌入式文档

[英]Replace Embedded Document using Mongo C# 2.0 driver

I have a document that have a embedded array of units, like this: 我有一个包含嵌入式单元数组的文档,如下所示:

{
    "_id" : ObjectId("5807c22e959ca231f0c48a75"),
    "Code" : "Value",
    "Units" : [ 
        {
            "_id" : ObjectId("5807cc08959ca331f09c530e"),
            "Code" : "Foo",
            "Label" : "Foo"
        },
        {
            "_id" : ObjectId("5807cc08959ca331f09c530e"),
            "Code" : "Bar",
            "Label" : "Bar"
        },
    ]
}

I want to find a unit by its id and replace it with a new document. 我想通过它的id找到一个单元并用一个新文件替换它。 How could I do that using the Mongo C# Driver 2.0? 我怎么能用Mongo C#Driver 2.0做到这一点?

Thanks in advance! 提前致谢!

Thanks guys. 多谢你们。 From your answers, I came up with the following solution: 根据您的回答,我想出了以下解决方案:

var mainDocumentId = "5807c22e959ca231f0c48a75";
var arrayItemId = "5807cc08959ca331f09c530e"; //Id for foo

var unit = new Unit(...)
var filter = Builders<UnitType>.Eq(o => o.Id, mainDocumentId); // optional
filter &= Builders<UnitType>.Filter.ElemMatch(o => o.Units, o => o.Id == arrayItemId);
var update = Builders<UnitType>.Update.Set(o => o.Units[-1], unit);

collection.UpdateOne(filter, update);

Assuming your models in C# look like this: 假设你在C#中的模型看起来像这样:

public class UnitLog
{
    [BsonRepresentation(BsonType.ObjectId)]
    [BsonId]
    public string Id { get; set; }

    public string Code { get; set; }

    public List<Unit> Units { get; set; }
}

public class Unit
{
    [BsonRepresentation(BsonType.ObjectId)]
    [BsonId]
    public string Id { get; set; }

    public string Code { get; set; }

    public string Label { get; set; }
}

You could use a PullFilter to pull an item from an embedded array. 您可以使用PullFilter从嵌入式阵列中提取项目。 Here is an example with the MongoDb 2.0 driver: 以下是MongoDb 2.0驱动程序的示例:

var mainDocumentId = "5807c22e959ca231f0c48a75";//Your containing document's Id
var arrayItemId = "5807cc08959ca331f09c530e";//Id for array item with Code=foo

var pullFilter = Builders<UnitLog>.Update.PullFilter(
        x => x.Units,
        x => x.Id == arrayItemId 
    );

//Note that you would use Collection.UpdateManyAsync()  
//if you expected more than one item to be updated/pulled
var result = await Collection.UpdateOneAsync(
        x => x.Id == mainDocumentId,
        pullFilter
    ).ConfigureAwait(false);

To identify items in an embedded array you would use $elemMatch for this 要识别嵌入数组中的项目,您可以使用$ elemMatch

ElemMatch MongoDb documentation ElemMatch MongoDb文档

ElemMatch can be found 可以找到ElemMatch

Builders<UnitLog>.Filter.ElemMatch 

And can be used in conjunction with $set to update individual properties of embedded documents, identified using $elemMatch. 并且可以与$ set一起使用来更新嵌入文档的各个属性,使用$ elemMatch进行标识。

Look into AddToSet, you may find this useful as it adds an item to an array unless the item exists already 查看AddToSet,您可能会发现这很有用,因为它会向项目添加项目,除非该项目已存在

AddToSet MongoDb documentation AddToSet MongoDb文档

AddToSet can be found 可以找到AddToSet

Builders<UnitLog>.Update.AddToSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM