Adding BSON array to BsonDocument in MongoDB

前端 未结 2 511
再見小時候
再見小時候 2020-12-31 00:58

How can I add BsonArray to BsonDocument in MongoDB using a C# driver? I want a result something like this

{ 
    author: \'joe\',
    title : \'Yet another b         


        
2条回答
  •  我在风中等你
    2020-12-31 01:41

    you can also add the array after the BsonDocument already exists, like this:

    BsonDocument  doc = new BsonDocument {
        { "author", "joe" },
            { "title", "yet another blog post" },
         { "text", "here is the text..." }
    };
    
    BsonArray  array1 = new BsonArray {
            "example", "joe"
        };
    
    
    BsonArray  array2 = new BsonArray {
            new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
            new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
        };
    
    
    doc.Add("tags", array1);
    doc.Add("comments", array2);
    

提交回复
热议问题