MongoDb C# 驱动操作示例

c#操作mongo数据库

驱动采用http://www.oschina.net/p/mongo-csharp-driver

C#驱动的基本数据库连接,增删改查操作
//定义对象
public class Person 
{
public ObjectId _id;

public string Name { get; set; }

public int Age { get; set; }

public override string ToString()
{
return string.Format("id:{0} Name:{1} Age:{2}", _id, Name, Age);
}
}

  

  
 
增加引用:
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

  

 

//连接数据库字符串
string connectionStr = "mongodb://localhost";
MongoClient client = new MongoClient(connectionStr);
MongoServer server = client.GetServer();
//选择数据库
MongoDatabase db = server.GetDatabase("person"); 
//选择文档集合
MongoCollection<BsonDocument> collection = db.GetCollection("Person");

  

//插入数据*******************************************************************

//方法1:
BsonDocument person = new BsonDocument();
person.Add("Name", "test");
person.Add("Age", 10); 
collection.Insert(person);

 
//方法2:
for (int i = 0; i < 100; i++)
{
var perSon = new Person()
{
Name = "test" + i,
Age = i
};
collection.Insert(perSon);
}

  

//查询数据*******************************************************************

//小于20
QueryDocument queryD1 = new QueryDocument("Age", new QueryDocument("$lt", 20)); //
foreach (var perSon in collection.Find(queryD1))
{
Console.WriteLine(perSon);
}
//等于 xq20 QueryDocument queryD2 = new QueryDocument("Name", "test20"); // foreach (var perSon in collection.Find(queryD2)) { Console.WriteLine(perSon); }
//等于 xq20 var query1 = Query.And(Query.EQ("Name","test20")); // foreach (var perSon in collection.Find(query1)) { Console.WriteLine(perSon); }

  

        

linq方式:

//Linq查询
var query2 = collection.AsQueryable<Person>().Where(n => n.Name.Contains("test")).Take(20).ToList();
//.Where(n => n.Name == "xixihaha").ToList();
foreach (var per in query2)
{
Console.WriteLine(per); 
}

  

  

//保存数据*******************************************************************

//Save1方法
var per = collection.AsQueryable<Person>().First(n => n.Name == "xixihaha");
//修改保存数据
per.Age = 50;
collection.Save(per);
per = collection.AsQueryable<Person>().First(n => n.Name == "xixihaha");
Console.WriteLine(per);

 

//Save2方法
var query = Query.And(Query.EQ("Name", "test5"));
var document = collection.FindOne(query);
if (document != null)
{
document["Age"] = 34;
collection.Save(document);
}
var per = collection.AsQueryable<Person>().First(n => n.Name == "test5");
Console.WriteLine(per);

 

//Update方法 
var query = Query.And(Query.EQ("Name", "test5"));
var update = Update.Set("Age", 45);
collection.Update(query, update);
var per = collection.AsQueryable<Person>().First(n => n.Name == "test5");
Console.WriteLine(per);

  

  

  ////删除数据******************************************************************* 

//删除指定文档
var query = Query.And(Query.EQ("Name", "test5"));

  

//删除所有文档
collection.RemoveAll();

  

源码地址:链接:http://pan.baidu.com/s/1b2OGGY 密码:cjp4

引用文档:http://www.cnblogs.com/wilber2013/p/4175825.html

posted on 2016-08-17 09:47  守护者  阅读(352)  评论(0编辑  收藏  举报