从MongoDB中删除记录
代码如下:
也可以用下面的代码删除记录,即只提供一个键值对:
获取记录
要获取一个特定的文件或记录,需要创建一个文档,并提供如下键值对:
FindOne()函数返回一个文档。若要获取记录的值,需要输入一个键值对来调用Get函数。
下面的源代码可供参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
namespace ConsoleApplication34
{
class Program
{
static void Main(string[] args)
{
//Create Database
Mongo mongoDBdataBase = new Mongo();
mongoDBdataBase.Connect();
var dataBaseToWork = mongoDBdataBase.GetDatabase("Bloggers");
//Create Collection
var blogger = dataBaseToWork.GetCollection("blogger");
//Insert Records
var b = new Document();
b["Name"] = "Dhananjay";
b["Country"] = "India";
blogger.Insert(b);
b["Name"] = "G Block";
b["Country"] = "USA";
blogger.Insert(b);
//Fetch Record
var searchBlogger = new Document();
searchBlogger["Name"] = "Dhananjay";
var result = blogger.FindOne(searchBlogger);
Console.WriteLine(result.Get("Country").ToString());
Console.ReadKey(true);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
namespace ConsoleApplication34
{
class Program
{
static void Main(string[] args)
{
//Create Database
Mongo mongoDBdataBase = new Mongo();
mongoDBdataBase.Connect();
var dataBaseToWork = mongoDBdataBase.GetDatabase("Bloggers");
//Create Collection
var blogger = dataBaseToWork.GetCollection("blogger");
//Insert Records
var b = new Document();
b["Name"] = "Dhananjay";
b["Country"] = "India";
blogger.Insert(b);
b["Name"] = "G Block";
b["Country"] = "USA";
blogger.Insert(b);
//Fetch Record
var searchBlogger = new Document();
searchBlogger["Name"] = "Dhananjay";
var result = blogger.FindOne(searchBlogger);
Console.WriteLine(result.Get("Country").ToString());
Console.ReadKey(true);
}
}
}
通过这种方式,就可以使用CSharp对MongoDB执行操作。