如何使用 C# 在 MongoDB 中的对象的项目数组中包含一个新项目?

如何使用 C# 在 MongoDB 中的对象的项目数组中包含一个新项目?


我尝试使用 AddToSet 方法,但没有成功。


我有以下代码结构:


1 - 父对象(Revenda):


using MongoDB.Bson;

using MongoDB.Bson.Serialization.Attributes;

using System.Collections.Generic;


namespace api.mstiDFE.Entidade.api.mstiDFE

{

    public class Revenda : Notificavel, IEntidade

    {


        public Revenda(string Id, long Codigo, string CPF, string CNPJ, List<RevendaCliente> Clientes)

        {

            this.Id = Id;

            this.Codigo = Codigo;

            this.CPF = CPF;

            this.CNPJ = CNPJ;

            this.Clientes = Clientes;

        }


        [BsonId]

        [BsonRepresentation(BsonType.ObjectId)]

        public string Id { get; private set; }


        [BsonElement("Codigo")]

        public long Codigo { get; private set; }


        [BsonElement("Nome")]

        public string Nome { get; private set; }


        [BsonElement("CPF")]

        public string CPF { get; private set; }


        [BsonElement("CNPJ")]

        public string CNPJ { get; private set; }


        [BsonElement("Clientes")]

        public ICollection<RevendaCliente> Clientes { get; private set; }

    }

}

2 - 子对象 (RevendaCliente):


using MongoDB.Bson;

using MongoDB.Bson.Serialization.Attributes;

using System.Collections.Generic;


namespace api.mstiDFE.Entidade.api.mstiDFE

{

    public class RevendaCliente : Notificavel, IEntidade

    {


        public RevendaCliente(string Codigo, string Nome, string CPF, string CNPJ, ICollection<RevendaClienteToken> Tokens)

        {

            this.Codigo = Codigo;

            this.Nome = Nome;

            this.CPF = CPF;

            this.CNPJ = CNPJ;

            this.Tokens = Tokens;

        }

    }

}

http://img1.mukewang.com/63afc04000014aa306550209.jpg

但是,如何才能在已在 MongoDB 中注册的父对象 (Revenda) 中只包含一个新的子对象 (RevendaCliente)?

我正在使用以下环境: -Microsoft.AspNetCore.App (2.1.1) -MongoDB.Driver (2.8.0)


斯蒂芬大帝
浏览 180回答 2
2回答

慕的地10843

(正如我在评论中提到的)您的问题看起来很简单,因为在 MongoDB 中,层次结构中的相关对象是同一文档的一部分,因此您需要更新内存中的对象并对其进行更新。var parentObject=CollRevendas.Find<Revenda>(revenda => revenda.Id == id).FirstOrDefault();parentObject.Clientes.Add(newChildObject);//now update the parent object

茅侃侃

对我有用的代码:(在 Aarif 的支持下解决)public bool AddRevendaCliente(string revendaId, RevendaCliente requestRevendaClient){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var filter = Builders<Revenda>.Filter.Eq(s => s.Id, revendaId);&nbsp; &nbsp; &nbsp; &nbsp; // Get a reference to the parent parent "Revenda"&nbsp; &nbsp; &nbsp; &nbsp; var parentObject = CollRevendas.Find<Revenda>(filter).FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; parentObject.Clientes.Add(requestRevendaClient);&nbsp; &nbsp; &nbsp; &nbsp; // Update the parent object "Revenda"&nbsp; &nbsp; &nbsp; &nbsp; var result = CollRevendas.ReplaceOneAsync(filter, parentObject);&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return true;}
打开App,查看更多内容
随时随地看视频慕课网APP