using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MongoDB.Bson; // Add BSON Library file
using MongoDB.Driver; // Add Driver Library file
namespace ConsoleApplication1
{
classProgram
{
staticvoid Main(string[] args)
{
MongoServer mongo = MongoServer.Create(); //Create object of MongoDB server
mongo.Connect(); // call to connect MongoDb server
Console.WriteLine("Connect to MongoDB Server");Console.WriteLine();
var db = mongo.GetDatabase("My_Database"); // Call to "My_ Collection" database which create in MongoDB
using (mongo.RequestStart(db)) // call to request send to MongoDB
{
var collection = db.GetCollection<BsonDocument>("My_Collection"); /* call to MongoDB which create collection
in "My_Database"*/
BsonDocument book = newBsonDocument() // Create BSON document which document name is "Book"
.Add("_id",BsonValue.Create(BsonType.ObjectId)) // Create "Object _id"
.Add("author","pradhan") // Add data in "book" document (key and value format)
.Add(" title","My exprience");
collection.Insert(book); /* Insert BSON document type data (name is "book") in collection ("My_collection") */
var query = newQueryDocument("author","pradhan"); /*Create Query for view data, which save in collection ("My_Collection"), in MongoDB database ("My_Databse") */
foreach (BsonDocument item in collection.Find(query)) // create foreach loop for select item in collection
{
Console.WriteLine("Key Value ");
Console.WriteLine();
foreach (BsonElement element in item.Elements) // create foreach loop for select element in item
{
Console.WriteLine("{0} {1}", element.Name, element.Value); // print element name and value
}
}
Console.WriteLine();
}
Console.WriteLine();
Console.Read();
mongo.Disconnect(); // call to disconnect MongoDB server
}
}
}