MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。 Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:
- 面向集合存储,易存储对象类型的数据。
- 模式自由。
- 支持动态查询。
- 支持完全索引,包含内部对象。
- 支持查询。
- 支持复制和故障恢复。
- 使用高效的二进制数据存储,包括大型对象(如视频等)。
- 自动处理碎片,以支持云计算层次的扩展性
- 支持RUBY,PYTHON,JAVA,C++,PHP等多种语言。
- 文件存储格式为BSON(一种JSON的扩展)
- 可通过网络访问
所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个 集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定 义任何模式(schema)。
模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。
存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized dOcument Format)。
MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB
在32位模式运行时支持的最大文件尺寸为2GB。
MongoDB把数据存储在文件中(默认路径为:/data/db),为提高效率使用内存映射文件进行管理。
下载地址为:
可选择window 32位mongodb,
也可以选择window 64位mongodb,
32-bit mongodb window 下载地址:
http://downloads.mongodb.org/win32/mongodb-win32-i386-2.0.2.zip
64-bit mongodb window 下载地址:
http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2.0.2.zip
其它:
Linux 32-bit,Linux 64-bit,etc...
本文主要针对window操作系统上安装mongodb分布式文档数据库系统,
下载后为zip压缩文件,然后解压后,在里面有一个bin目录,里面就是运行的系统文件
注意:32位的mongodb数据库最多只能为2G的限制,这是由32位操作系统的内存寻址空间能力确定的,
所在要存储海量数据库数据必须选择64位操作系统安装64位mongodb数据库
建立启动的批处理文件:
1.命令行方式启动服务器端
echo "bat_setupService.bat"
mongod.exe -dbpath D:\Mongo_Service\db\
2 命令行方式启动客户端
echo "bat_setupClient.bat"
mongo.exe cclove
3安装为window服务方式
echo "bat_installService.bat "
mongod --install --serviceName MongoDB --logpath D:\MongoDB_Service\MongoDB.Log --dbpath D:\MongoDB_Service\db --directoryperdb
然后启动window服务MongoDB就开始运行了
(四)MongoDB C#测试代码
001 | public class TestClass:IDisposable |
003 | private string connectionstring = "" ; |
004 | private MongoServer mserver = null ; |
008 | host = "192.168.1.221" ; |
012 | MongoConnectionStringBuilder mcsb = new MongoConnectionStringBuilder(); |
013 | mcsb.Server = new MongoServerAddress(host, port); |
014 | mcsb.ConnectTimeout = new TimeSpan(30000); |
015 | mcsb.MaxConnectionLifeTime = new TimeSpan(300000); |
016 | mcsb.MinConnectionPoolSize = 8; |
017 | mcsb.MaxConnectionPoolSize = 2000; |
026 | this .mserver = MongoServer.Create(mcsb); |
030 | public int mongodbconn( int couts) |
035 | MongoDatabase db = this .mserver.GetDatabase( "test" ); |
037 | MongoCollection<BsonDocument> mc = db.GetCollection<BsonDocument>( "test" ); |
041 | DateTime begintime = DateTime.Now; |
042 | writefile( "bgn: " +begintime.ToString()); |
044 | for ( i = 1; i <= couts; i++) |
046 | BsonDocument bd = new BsonDocument |
051 | { "title" , "testing inert data " +i } |
054 | DateTime midtime = DateTime.Now; |
057 | writefile( "row: " +i+ " value=" +bd.ToString()); |
058 | TimeSpan tso = midtime.Subtract(begintime); |
059 | string stimeo = tso.TotalMilliseconds.ToString(); |
060 | writefile( "mid: " + stimeo+ " ms" ); |
064 | DateTime endtime = DateTime.Now; |
065 | writefile( "end: " +endtime.ToString()); |
066 | TimeSpan ts = endtime.Subtract(begintime); |
067 | string stime=ts.TotalMilliseconds.ToString(); |
068 | writefile( "use " + stime + " ms to insert " +i+ " rows" ); |
073 | MessageBox.Show(ex.Message); |
079 | public static bool writefile( string str) |
081 | string startpath = Application.StartupPath; |
082 | string logpath=startpath+ "\\mongodb.log" ; |
083 | StreamWriter sw = new StreamWriter(logpath, true ); |
090 | public bool query( int type, string type1value, string typeothervalue) |
096 | MongoDatabase db = mserver.GetDatabase( "test" ); |
097 | MongoCollection<BsonDocument> mc = db.GetCollection<BsonDocument>( "test" ); |
104 | sql = typeothervalue; |
106 | DateTime qtime = DateTime.Now; |
107 | var query = new QueryDocument( "_id" ,Convert.ToInt32(sql)); |
109 | writefile( "begin: " + qtime.ToString()); |
110 | foreach (BsonDocument emp in mc.Find(query).SetLimit(100)) |
114 | string rename = emp[ "name" ].AsString; |
115 | string revalue = emp[ "title" ].AsString; |
116 | writefile(rename + " " +revalue+ " value=" +emp.ToString()); |
118 | DateTime etime = DateTime.Now; |
119 | TimeSpan ts = etime.Subtract(qtime); |
120 | string stime = ts.TotalMilliseconds.ToString(); |
121 | writefile( "use " + stime + " ms to query " +counts+ "data rows" ); |
126 | MessageBox.Show(ex.Message); |
132 | #region IDisposable 成员 |
134 | public void Dispose() |
136 | if ( this .mserver != null ) |
138 | this .mserver.Disconnect(); |
调用引类的方法
01 | public partial class Form1 : Form |
05 | InitializeComponent(); |
09 | private void btn_ConnMongoDb_Click(object sender, EventArgs e) |
11 | TestClass test = new TestClass(); |
13 | test.query( 1 , "1" , "2" ); |
15 | MessageBox.<SPAN class = "goog_qs-tidbit goog_qs-tidbit-1" >Show( "连接,插入,查询操作完毕!</SPAN>" , "提示" ); |
需要在C#开发的项目工程中添加两个.Net的组件dll
MongoDB.Bson
MongoDB.Driver
这个需要安装MongoDB的C#驱动程序
需到下面的网址去下载:
我下载的是这个版本:CSharpDriver-1.3.1.4349.msi
下载下来安装后,就可以通过C#工程中的引用/Net组件
引用这两个组件了
MongoDB.Bson
MongoDB.Driver
------the----end-------
create date:2012-02-22
creater:hsg