Below code shows how to insert a record into MongoDB using Asp.net.
First install the MongoDB driver from the nuget package manager.
using MongoDB.Bson;
using MongoDB.Driver;
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection
string
static IMongoDatabase db = client.GetDatabase("test"); //Database name
public void InsertCollection()
{
BsonDocument doc = new BsonDocument //Create BSON document object
{
{ "name" , "me" },
{"age" , "99"},
{"addr" , new BsonDocument { {"pin" , "1234"} , {"state","unknown"} }}
};
IMongoCollection<BsonDocument>
collection = db.GetCollection<BsonDocument>("user"); //Table name
collection.InsertOne(doc);
}
No comments:
Post a Comment