TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
bapu
NA
13
0
Inserting data from string list into mongodb c#
Jan 27 2017 6:32 PM
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
Reply
Answers (
3
)
How to run through excelfile and group rows
Error exception startindex cannot be less than zero