Hello,
I am new to mongoDB and C# and I am having some issue with inserting data from string list into mongoDB using C#
Class
public class Person
{
public List<string> Title = new List<string>();
public List<string> FirstName = new List<string>();
public List<string> LastName = new List<string>();
public List<int> Age = new List<int>();
}
Test Data
p.Title.Add("Mr.");
p.Title.Add("Mrs");
p.FirstName.Add("Person 1");
p.FirstName.Add("Person 2");
p.LastName.Add("Last Name 1");
p.LastName.Add("Last Name 2");
p.Age.Add(34);
p.Age.Add(30);
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("Test2");
var customer = database.GetCollection("Address");
BsonDocument document2 = new BsonDocument {
{ "Address", "Address 1" },
{ "Members", new BsonArray {
new BsonDocument
{
{ "Name", p.FirstName.ToString())},
{ "Age", p.Age.ToString())}
}
}}
};
customer.Insert(document2);
it inserts data but for Name & Age it does not insert list data just inserts System.Collections.Generic.List1
I would like to enter data like in bellow JSON
{
"Address" : "this is address 1",
"Family Members" : [
{
"Name" : "Person 1",
"age" : 34
},
{
"Name" : "Person 2",
"age" : 30
}
]
}
person can vary it might be 2 or 3 or 4
Any help? please
Thank you