Insert documents
Insert one document
IMongoCollection<T>.InsertOne(<document>)var database = Client
.GetDatabase(Constants.SamplesDatabase);
var personsCollection = database
.GetCollection<User>(Constants.UsersCollection);
User appPerson = RandomData.GenerateUsers(1).First();
// Insert one document
await personsCollection.InsertOneAsync(appPerson);var personsBsonCollection = usersDatabase.GetCollection<BsonDocument>("users");
var bsonPerson = new BsonDocument
{
{ "gender" , 1 },
{ "firstName" , "Johh" },
{ "lastName" , "Doe"},
{ "userName" , "Kimberly12"},
{ "avatar" , "https://api.adorable.io/avatars/285/abott@adorable.png" },
{ "email" , "Kimberly29@hotmail.com"},
{ "dateOfBirth" , new BsonDateTime(DateTime.Now.AddYears(-25)) },
{ "address", new BsonDocument
{
{"street" , "113 Al Points" },
{"suite" , "Apt. 697" },
{"city" , "South Murrayshire" },
{"state" , "South Dakota" },
{"zipCode" , "35038" },
{
"geo", new BsonDocument()
{
{ "lat", 87.333 },
{ "lng", -116.99 }
}
}
}
},
{ "phone" , "392-248-7338 x083" },
{ "website" , "terry.biz" },
{
"company" , new BsonDocument()
{
{"name" , "Bode - Hills" },
{"catchPhrase" , "Total composite service-desk" },
{"bs" , "morph customized bandwidth"}
}
},
{ "salary" , 1641 },
{ "monthlyExpenses" , 3009 },
{ "favoriteSports", new BsonArray{ "Basketball", "MMA" } },
{ "profession", "Doctor" }
};var personsBsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
User appPerson = RandomData
.GenerateUsers(1).First();
await personsBsonCollection
.InsertOneAsync(appPerson.ToBsonDocument());Insert many documents
Last updated