linux下C#开发mongoDB

首先按  http://www.cnblogs.com/guoyuanwei/archive/2012/04/04/2432013.html

介绍的方法安装好monodevelop和mono开发环境

按照mongoDB官方的例子安装好mongoDB在linux上:http://www.mongodb.org/display/DOCS/Quickstart+Unix注意根据CPU是32位还是64位下载不同的版本

打开一个终端启动mongoDB的数据库服务 root@ubuntu:/usr/local/mongoDB/bin# ./mongod

在接下来的过程中,创建一个数据库gywdb来做测试。奇怪的事情是mongoDB没有直接创建数据库的命令,找了半天没找到,后来只有通过间接的方式来创建。如下命令所示。

打开一个新的终端:默认连接到的数据库是test

root@ubuntu:/usr/local/mongoDB/bin# ./mongo
MongoDB shell version: 2.0.4
connecting to: test

创建自己的数据库gywdb

 use gywdb
switched to db gywdb
> db.createCollection("student",{});
{ "ok" : 1 }
这样数据库gywdb创建成功了,而且创建了一个student的表。

下面是通过C#来操作mongoDB的一些代码

运行命令 show dbs 检查下数据库是否创建成功。
gywdb    0.203125GB
local    (empty)
test    0.203125GB
可以看到gywdb创建好了。

下面是通过C#驱动代码来实现mongoDB的各种操作。

1、查询服务器中所有存在的数据库

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer 
server=MongoServer.Create(con);                
            IEnumerable<string> names=server.GetDatabaseNames();
            foreach(string name in names)
                Console.WriteLine(name);
            Console.ReadLine();
        }
    }
}

运行结果:

2、插入文档数据到数据表student中去

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");    
            //获得数据库中的表对象,即student表
            MongoCollection mydbTable=mydb.GetCollection("student");
            //准备一条数据,即申明一个文档对象
            BsonDocument doc=new BsonDocument
            {
                {"name","令狐少侠"},
                {"classname","华山派"},
                {"age",100}
            }; 
            //将文档插入,持久化到硬盘上
            mydbTable.Insert(doc);            
                 Console.ReadLine();
        }
    }
}

通过命令查看结果: > db.student.find();
{ "_id" : ObjectId("4f852ce41d41c80d9b090110"), "name" : "令狐少侠", "classname" : "华山派", "age" : 100 }

可以看到表中有刚才通过代码加入的一条数据了,其中字段“_id”为系统自动生成的,相当于一个主键。

3、查询数据,先通过上面的插入代码,插入几条测试数据

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");    
            //获得数据库中的表对象,即student表
            MongoCollection mydbTable=mydb.GetCollection("student");

            //返回一个游标,游标是系统为用户开设的一个数据缓冲区,用来存放查询语句返回的结果
            MongoCursor<BsonDocument> listDoc=mydbTable.FindAllAs<BsonDocument>();//返回things表中的所有记录
            foreach(BsonDocument bd in listDoc)
            {
              Console.WriteLine(bd);
            }            

    }
}

运行结果如下图:

 4、更新数据,将上面结果中的name=“令狐少侠”的“classname”修改为“恒山派”

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");    
            //获得数据库中的表对象,即student表
            MongoCollection<BsonDocument> mydbTable=mydb.GetCollection<BsonDocument>("student");
            
            //定义一个查询对象,相当于SqlServer中的where条件语句
            QueryDocument queryDoc=new QueryDocument{{"name","令狐少侠"}};
            //定义一个跟新对象,相当于sqlServer中的set语句
            UpdateDocument updateDoc=new UpdateDocument{{"$set",new BsonDocument("classname","恒山派")}};
            //将查询对象和跟新对象作为参数传递给表,调用update来完成跟新
            mydbTable.Update(queryDoc,updateDoc);
}
}
}

 

运行后通过命令查看结果:

可以看到修改成功。

 5、完成了基本的数据库增删改查外,mongoDB还提供了一个非常具有吸引的东西,那就是分布式文件系统规范,GridFS

      GridFS会将一个大文件分割成多个小文件,它使用两个表来存储数据。files表包含元数据的对象,chunks包含其他一些相关的信息的二进制块,下面现通过代码将本地文件系统中的文件上传到mongoDB的GridFS中去。

 

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");    
            //定义一个本地文件的路径字符串
            string localFileName="/home/guoyuanwei/学习资料/百度大规模数据处理.pdf";
            //定义mongoDB数据库中文件的名称
            string mongoDBFileName="百度大规模数据处理";
            //将本地文件上传到mongoDB中去
            mydb.GridFS.Upload(localFileName,mongoDBFileName);
            }
}
}

 

执行完毕后,通过命令> show collections,得到如下结果
fs.chunks
fs.files
student
system.indexes
发现系统中多了两个表fs.chunks和fs.files,其中fs.files存的是文件的元数据信息,通过命令:> db.fs.files.find()查看如下:

{ "_id" : ObjectId("4f853a521d41c80e8c56192b"), "filename" : "百度大规模数据处理", "length" : 588276, "chunkSize" : 262144, "uploadDate" : ISODate("2012-04-11T08:01:22.508Z"), "md5" : "5e55fb7496d41a52eb90daeac9e06936" }

参数意义如下:filename:存储的文件名,chunkSize:chunk的大小,upload加入时间,md5文件md5码,length:文件的大小。

接着通过命令db.fs.chunks.find()查看表fs.chunks中的信息
{ "_id" : ObjectId("4f8540a51d41c80ef7892957"), "files_id" : ObjectId("4f8540a51d41c80ef7892954"), "n" : 0, "data" : BinData(0,"TW9uZ29Db2xsZWN0aW9uPEJzb25Eb2N1bWVudD4gY29sbD1teWRiLkdldENvbGxlY3Rpb248......QnNvbkRvY3VtZW50PigidGhpbmdzIik7Cg==") }

其中字段n表示块的序号,从0开始,data表示的是实际存储的信息。 中间省略号表示数据太长了,没写出来

接着执行> db.fs.chunks.count()
1
说明表示块信息的表chunks中只有一个块,因为实际的文件小于256KB,所以文件没有被分割。
下面换一个大的文件测试。

代码如下:

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");    
            //定义一个本地文件的路径字符串
            string localFileName="/home/guoyuanwei/学习资料/Google三大论文中文版.pdf";
            //定义mongoDB数据库中文件的名称
            string mongoDBFileName="Google三大论文中文版";
            //将本地文件上传到mongoDB中去
            mydb.GridFS.Upload(localFileName,mongoDBFileName);
}
}
}

其中Google三大论文中文版.pdf大小约为2.4MB,按照上面分析的块的大小为256KB,算出应该有10个chunks(块)

运行代码后,执行命令查看:

> db.fs.chunks.count()
10
为10,与上面的计算符合。

6、上面把一个2.4MB的文件放到GridFS中去了,接下来在做个把文件取出来,保存到本地文件系统的实验

 

View Code
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;

namespace mongoDBClient
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //mongoDb服务实例连接字符串
            string con="mongodb://localhost:27017";
            //得到一个于mongoDB服务器连接的实例
            MongoServer server=MongoServer.Create(con);            
            //获得一个与具体数据库连接对象,数据库名为gywdb
            MongoDatabase mydb=server.GetDatabase("gywdb");                
            //定义mongoDB数据库中文件的名称
            string mongoDBFileName="Google三大论文中文版";
            //定义一个本地文件的路径字符串
            string localFileName="/home/guoyuanwei/学习资料/New_Google三大论文中文版.pdf";
            //从mongoDB中读取文件,并保存到本地硬盘上
            mydb.GridFS.Download(localFileName,mongoDBFileName);
}
}
}

 

 

   

posted @ 2012-04-11 17:03  郭远威  阅读(2718)  评论(4编辑  收藏  举报