MongoDB--CSharp Driver Quickstart .
2013-09-09 11:01
302 查看
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart?showComments=true&showCommentArea=true#addcomment
1. MongoDB.Bson.dll
2. MongoDB.Driver.dll
[html] view plaincopyprint?
using MongoDB.Bson;
using MongoDB.Driver;
另外你也许需要下面这些命名空间
[html] view plaincopyprint?
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
[html] view plaincopyprint?
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
你应该在你的连接字符串中始终加上"safe=true"
或许你想用一个全局变量来保存你的服务器对象,其实当你使用相同的连接字符调用MongoServer.Create方法时返回的服务器对象是相同的对象引用,因此你无需采用全局变量来保存你的服务器对象在需要的时候调用MongoServer.Create方法即可。
[html] view plaincopyprint?
var database = server.GetDatabase("test"); // "test" is the name of the database
如果你需使用更多的数据库只需要多次调用server.GetDatabase()方法即可。
1.使用BsonDocument对象。
2.使用自定义的类对象。
当你要处理的数据格式比较灵活难以用自定义的类来描述时你应该考虑采用BsonDocument来操作集合.
采用自定义的类对象可以让其他开发人员快速了解你的意图,自定义的类需要满足如下两个条件:
1.需要一个没有参数的构造函数。
2.定义公开的可读写的字段或属性来操作数据库中表相关的字段。
另外如果你要用自定义类操作根文档,你的类还必须包含一个ID属性。通常情况下id的类型为ObjectId。
[html] view plaincopyprint?
var collection = database.GetCollection<Entity>("entities"); // "entities" is the name of the collection
[html] view plaincopyprint?
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
[html] view plaincopyprint?
var query = Query.EQ("_id", id);
var entity = collection.FindOne(query);
[html] view plaincopyprint?
entity.Name = "Dick";
collection.Save(entity);
[html] view plaincopyprint?
var query = Query.EQ("_id", id);
var update = Update.Set("Name", "Harry"); // update modifiers
collection.Update(query, update);
[html] view plaincopyprint?
var query = Query.EQ("_id", id);
collection.Remove(query);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace ConsoleApplication1
{
public class Entity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id;
var query = Query.EQ("_id", id);
entity = collection.FindOne(query);
entity.Name = "Dick";
collection.Save(entity);
var update = Update.Set("Name", "Harry");
collection.Update(query, update);
collection.Remove(query);
}
}
}
介绍
本文的目的是让你对如何使用Mongodb的C#驱动有一个简单的了解,当你阅读完本文你就可以参考其他文章以了解更多信息。下载C#驱动
你可以在这里下载需要的驱动。网站提供压缩包(zip)和安装包(msi)两种格式的驱动,下载完成后你可以将他解压或安装到你需要的目录。添加C#驱动
在你的visual studio项目右击项目选择"Add Reference..."将如下两个dll添加到项目的引用中.1. MongoDB.Bson.dll
2. MongoDB.Driver.dll
添加需要的命名空间
你的项目要使用MongoDB,至少需要下面两个命名空间[html] view plaincopyprint?
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson; using MongoDB.Driver;
另外你也许需要下面这些命名空间
[html] view plaincopyprint?
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS; using MongoDB.Driver.Linq;
获取服务器对象
获取服务器的连接对象非常简单[html] view plaincopyprint?
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString);
你应该在你的连接字符串中始终加上"safe=true"
或许你想用一个全局变量来保存你的服务器对象,其实当你使用相同的连接字符调用MongoServer.Create方法时返回的服务器对象是相同的对象引用,因此你无需采用全局变量来保存你的服务器对象在需要的时候调用MongoServer.Create方法即可。
获取数据库对象
获取数据库对象的方法如下:[html] view plaincopyprint?
var database = server.GetDatabase("test"); // "test" is the name of the database
var database = server.GetDatabase("test"); // "test" is the name of the database
如果你需使用更多的数据库只需要多次调用server.GetDatabase()方法即可。
如何确定使用BsonDocument对象还是自定义类对象
你有两种方式来操作一个集合1.使用BsonDocument对象。
2.使用自定义的类对象。
当你要处理的数据格式比较灵活难以用自定义的类来描述时你应该考虑采用BsonDocument来操作集合.
采用自定义的类对象可以让其他开发人员快速了解你的意图,自定义的类需要满足如下两个条件:
1.需要一个没有参数的构造函数。
2.定义公开的可读写的字段或属性来操作数据库中表相关的字段。
另外如果你要用自定义类操作根文档,你的类还必须包含一个ID属性。通常情况下id的类型为ObjectId。
获取集合对象
为了更好理解这个小节,我们采用一个名叫Entity的自定义类,你可以使用如下方式获取包含Entity文档的集合。[html] view plaincopyprint?
var collection = database.GetCollection<Entity>("entities"); // "entities" is the name of the collection
var collection = database.GetCollection<Entity>("entities"); // "entities" is the name of the collection
插入一个文档
插入一个文档的方式非常简单[html] view plaincopyprint?
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
var entity = new Entity { Name = "Tom" }; collection.Insert(entity); var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
查找文档
在这个列子中我将根据指定的id来获取相应的文档信息。[html] view plaincopyprint?
var query = Query.EQ("_id", id);
var entity = collection.FindOne(query);
var query = Query.EQ("_id", id); var entity = collection.FindOne(query);
保存文档
你可以采用如下方式来保存一个已更改的文档[html] view plaincopyprint?
entity.Name = "Dick";
collection.Save(entity);
entity.Name = "Dick"; collection.Save(entity);
更新已存在的文档
另一种保存的方式就是更新。两种方法的不同点是Save()方法是将整个文档数据发回给服务器,而update()只是将更改的信息返回给服务器。[html] view plaincopyprint?
var query = Query.EQ("_id", id);
var update = Update.Set("Name", "Harry"); // update modifiers
collection.Update(query, update);
var query = Query.EQ("_id", id); var update = Update.Set("Name", "Harry"); // update modifiers collection.Update(query, update);
删除已存在的文档
要删除集合中的一条文档信息写法如下:[html] view plaincopyprint?
var query = Query.EQ("_id", id);
collection.Remove(query);
var query = Query.EQ("_id", id); collection.Remove(query);
你无需调用Connect() 或 Disconnect() 方法
C#驱动有个连接池以高效的管理服务器连接。你无需调用Connect() 或 Disconnect()方法,驱动会自动管理连接。调用Connect()方法没有什么坏处,但是不能调用Disconnect()方法因为调用该方法会关闭连接池中的所有连接。完整的例子代码
[html] view plaincopyprint?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace ConsoleApplication1
{
public class Entity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id;
var query = Query.EQ("_id", id);
entity = collection.FindOne(query);
entity.Name = "Dick";
collection.Save(entity);
var update = Update.Set("Name", "Harry");
collection.Update(query, update);
collection.Remove(query);
}
}
}
相关文章推荐
- MongoDB--CSharp Driver Quickstart
- MongoDB-02 Quickstart Windows 在Windows上试一下吧
- Mongodb CSharp Driver Tutorial
- nodejs中使用mongodb quickstart
- mongo-java-driver -3.2.2学习笔记-01-MongoDB Driver Quick Tour
- mongo-java-driver -3.2.2学习笔记-02-MongoDB Driver Admin Quick Tour
- mongodb mongo csharp driver 源码分析(1)
- mongodb java Driver 3.5.0 POJOs
- PHP code coverage QuickStart
- struts quick start
- WPF/E CTP Quick Start - 第四部分:绘图与填充(翻译)
- Cloudera: Start Impala service by cloudera manager in docker quickstart image
- Install Centrifugo and quick start
- spark学习笔记之quick-start
- mongodb c driver
- VS2015编译连接mongodb(mongodb C++ driver)
- Apache Mina 2.0.9 Quick Start Guide
- MongoDB_C_Driver使用教程(2)高级连接
- MongoDB Node.js driver
- add mongodb service to centos boot start