mongodb mongo csharp driver下载地址:https://github.com/mongodb/mongo-csharp-driver/downloads 

具体的下载地址可以到mongodb官方网站上进行查找。

下面例子是一个简单的查询:

//创建数据库连接,默认为本地连接端口为27017
MongoServer server = MongoServer.Create();
//获取数据库,如果没有会自动创建
MongoDatabase database = server.GetDatabase("MyDatabase");
//获取collection集合
MongoCollection things = database.GetCollection("things");
try
{
//打开连接
server.Connect();
//设置查询条件 x like (3,4)
QueryConditionList condition = new QueryConditionList("x");
condition.In(
new BsonArray().Add(new BsonInt32(3)).Add(new BsonInt32(4)));
MongoCursor
<BsonDocument> cur = things.FindAs<BsonDocument>(condition);
foreach (BsonDocument item in cur)
{
MessageBox.Show(item[
"j"].AsDouble.ToString());
}
}
finally
{
//关闭连接
server.Disconnect();
}
在mongo csharp driver中,主要有Driver和Bson包。上面的例子,我们使用一几个类

MongoServer

MongoDatabase

MongoCollection

QueryConditionList

MongoCursors

后续将首先分析这几个类,进行实现对数据库的增、删、改、查功能。

posted on 2011-04-08 11:33  lirenqing  阅读(922)  评论(0编辑  收藏  举报