Fork me on GitHub

.NET平台开发Mongo基础知识

NoSQL简介

NoSQL相关的技术最近越来越受欢迎,Mongo本身就是基于NoSQL实现的。关于NoSQL你需要了解

  1. 什么是NoSQL
  2. NoSQL和传统的关系型数据库有什么区别
  3. NoSQL的优缺点

这几个问题下面的文章有所介绍:

http://www.runoob.com/mongodb/nosql.html

http://www.infoq.com/cn/news/2011/01/nosql-why/

MongoDB

MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

更详细的介绍可以参见:

http://www.runoob.com/mongodb/mongodb-intro.html

MongoDB安装

http://www.mongodb.org/downloads 地址下载并安装MongoDB。

根据你的机器选择相应的安装包,32位系统上MongoDB数据库最大为2G。

安装结束后首先创建默认的数据库存储地址c:\data\db

通过命令行mongod.exe --dbpath c:\data\db来把mongodb和存储路径关联起来,命令行显示如下:

表示关联成功,并且在27017端口上监听连接。

MongoDB后台管理Shell

在安装路径下执行mongo.exe文件会执行MongoDB Shell,是一个自带的交互式的JavaScript Shell,用来对MongoDB进行操作和管理的交互式环境。

Shell中输入help显示帮助命令

数据库创建删除

  1. Shell中输入 use tutorial来尝试连接名为tutorial的数据库,如果数据库不存在则创建。
  2. 输入db.dropdatabase()来删除当前数据库。
  3. 输入show dbs显示数据库信息。

创建删除表信息

  1. db.websites.insert({title:'www.baidu.com',url:'www.baidu.com'}) 来在websites表中插入一条记录
  2. db.websites.find()查询

 

C#操作MongoDB

首先下载.NET版本的MongoDB Driver,尝试在Nuget找一下吧:

下载并安装。

找到了API我们就可以进行增删改查了,下面的Demo Code展示了基本的数据库操作。

 

using System;
using System.Xml.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WikiExampleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Connect...");

            MongoConnectionStringBuilder builder = new MongoConnectionStringBuilder();
            builder.Server = new MongoServerAddress("localhost", 27017);
            builder.DatabaseName = "tutorial";

            MongoServer mongo = MongoServer.Create(builder);
            mongo.Connect();

            Console.WriteLine("Connected"); Console.WriteLine();

            var db = mongo.GetDatabase("tutorial");

            using (mongo.RequestStart(db))
            {
                var collection = db.GetCollection<BsonDocument>("books");

                BsonDocument book = new BsonDocument()
                    .Add("_id", BsonValue.Create(BsonType.ObjectId))
                    .Add("author", "Ernest Hemingway")
                    .Add("title", "For Whom the Bell Tolls");

                collection.Insert(book);

                var query = new QueryDocument("author", "Ernest Hemingway");

                foreach (BsonDocument item in collection.Find(query))
                {
                    string json = item.ToJson();

                    Console.WriteLine(json);
                    Console.WriteLine();

                    JToken token = JToken.Parse(json);
                    token.SelectToken("title").Replace("some other title");

                    Console.WriteLine("Author: {0}, Title: {1}", token.SelectToken("author"), token.SelectToken("title"));
                    Console.WriteLine();

                    XNode node = JsonConvert.DeserializeXNode(json, "documents");

                    Console.WriteLine("Node:");
                    Console.WriteLine(node);
                    Console.WriteLine();

                    BsonElement author = item.GetElement("author");
                    BsonElement title = item.GetElement("title");

                    foreach (BsonElement element in item.Elements)
                    {
                        Console.WriteLine("Name: {0}, Value: {1}", element.Name, element.Value);
                    }

                    Console.WriteLine();
                    Console.WriteLine("Author: {0}, Title: {1}", author.Value, title.Value);
                }
            }

            Console.WriteLine();
            Console.Read();

            mongo.Disconnect();
        }
    }
}

 

相关代码托管在了https://github.com/cuicheng11165/Mongo-Demo上。

 

posted @ 2016-01-21 14:56  独上高楼  阅读(1133)  评论(0编辑  收藏  举报