MongoDB add array to BsonDocument

I am trying to add fields to a mongodb document using C # drivers.

I am creating a document.

BsonDocument document = new BsonDocument();

and adding

document.Add(name, value); // here name and value both are string

but I'm not sure how to add an array to this script

as document.Add(name, values); // here values is List<string>

eg. document.Add(skills, [C++, Java, C#]);

please help me with this

+4
source share
1 answer

If you work with List<string>:

var skills = new List<string> {"C++", "Java", "C#"};
document.Add("skills", new BsonArray(skills));

Or simply put:

document.Add("skills", new BsonArray { "C++", "Java", "C#" } );
+5
source

Source: https://habr.com/ru/post/1526359/


All Articles