第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 C# 在 MongoDB 中的對象的項目數(shù)組中包含一個新項目?

如何使用 C# 在 MongoDB 中的對象的項目數(shù)組中包含一個新項目?

C#
斯蒂芬大帝 2022-12-31 12:51:58
如何使用 C# 在 MongoDB 中的對象的項目數(shù)組中包含一個新項目?我嘗試使用 AddToSet 方法,但沒有成功。我有以下代碼結(jié)構(gòu):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;        }    }}但是,如何才能在已在 MongoDB 中注冊的父對象 (Revenda) 中只包含一個新的子對象 (RevendaCliente)?我正在使用以下環(huán)境: -Microsoft.AspNetCore.App (2.1.1) -MongoDB.Driver (2.8.0)
查看完整描述

2 回答

?
慕的地10843

TA貢獻1785條經(jīng)驗 獲得超8個贊

(正如我在評論中提到的)您的問題看起來很簡單,因為在 MongoDB 中,層次結(jié)構(gòu)中的相關對象是同一文檔的一部分,因此您需要更新內(nèi)存中的對象并對其進行更新。


var parentObject=CollRevendas.Find<Revenda>(revenda => revenda.Id == id).FirstOrDefault();

parentObject.Clientes.Add(newChildObject);

//now update the parent object


查看完整回答
反對 回復 2022-12-31
?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超22個贊

對我有用的代碼:(在 Aarif 的支持下解決)


public bool AddRevendaCliente(string revendaId, RevendaCliente requestRevendaClient)

{

    try

    {

        var filter = Builders<Revenda>.Filter.Eq(s => s.Id, revendaId);


        // Get a reference to the parent parent "Revenda"

        var parentObject = CollRevendas.Find<Revenda>(filter).FirstOrDefault();

        parentObject.Clientes.Add(requestRevendaClient);


        // Update the parent object "Revenda"

        var result = CollRevendas.ReplaceOneAsync(filter, parentObject);

    }

    catch (Exception ex)

    {

        throw;

    }            

    return true;

}


查看完整回答
反對 回復 2022-12-31
  • 2 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號