6

I am trying to add fields in 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 am not sure how to add an array in this scenario

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

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

please help me with this

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
Sameer Rathoud
  • 133
  • 3
  • 8

1 Answers1

7

If you're working with a List<string>:

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

Or, more simply:

document.Add("skills", new BsonArray { "C++", "Java", "C#" } );
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88