MongoVUE, 一个还算不错的 mongodb 管理器,有只读 free 版本.
mongod.exe --dbpath D:\mongodb-win32-i386-2.0.2\bin\data
C#中使用MongoDb
首先从网上下载MongoDB。地址http://www.mongodb.org/,找到适合自己的下载
这是我下载的。
在E盘新建个文件夹,将刚才下载的zip解压,将其中bin目录下的文件全部拷贝至刚才新建的文件夹。
然后在其中再建立个data文件夹。
然后通过cmd去启动你的MongoDB
看我红线框出来的即可。上面打错了 - -
然后访问localhost:27017看到如下所示,就表示你的MongoDB已经启动完毕
增加:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.Data.SqlClient;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//连接信息
string conn = "mongodb://localhost";
string database = "demoBase";
string collection = "demoCollection";
MongoServer mongodb = MongoServer.Create(conn);//连接数据库
MongoDatabase mongoDataBase = mongodb.GetDatabase(database);//选择数据库名
MongoCollection mongoCollection = mongoDataBase.GetCollection(collection);//选择集合,相当于表
mongodb.Connect();
//普通插入
var o = new { Uid = 123, Name = "xixiNormal", PassWord = "111111" };
mongoCollection.Insert(o);
//对象插入
Person p = new Person { Uid = 124, Name = "xixiObject", PassWord = "222222" };
mongoCollection.Insert(p);
//BsonDocument 插入
BsonDocument b = new BsonDocument();
b.Add("Uid", 125);
b.Add("Name", "xixiBson");
b.Add("PassWord", "333333");
mongoCollection.Insert(b);
Console.ReadLine();
}
}
class Person {
public int Uid;
public string Name;
public string PassWord;
}
}
结果:
都是上述配置写的,程序会自动建立对应的库和集合。
下面的操作不上完整代码了:
/*---------------------------------------------
* sql : SELECT * FROM table
*---------------------------------------------
*/
MongoCursor<Person> p = mongoCollection.FindAllAs<Person>();
/*---------------------------------------------
* sql : SELECT * FROM table WHERE Uid > 10 AND Uid < 20
*---------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);
/*-----------------------------------------------
* sql : SELECT COUNT(*) FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
long c = mongoCollection.Count(query);
/*-----------------------------------------------
* sql : SELECT Name FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);
FieldsDocument f = new FieldsDocument();
f.Add("Name", 1);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query).SetFields(f);
/*-----------------------------------------------
* sql : SELECT * FROM table ORDER BY Uid DESC LIMIT 10,10
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
SortByDocument s = new SortByDocument();
s.Add("Uid", -1);//-1=DESC
MongoCursor<Person> m = mongoCollection.FindAllAs<Person>().SetSortOrder(s).SetSkip(10).SetLimit(10);