首先從網上下載MongoDB。地址http://www.mongodb.org/,找到適合自己的下載
這是我下載的。
在E盤建立個檔案夾,將剛才下載的zip解壓,將其中bin目錄下的檔案全部拷貝至剛才建立的檔案夾。
然後在其中再建立個data檔案夾。
然後通過cmd去啟動你的MongoDB
看我紅線框出來的即可。上面打錯了 - -
將mongodb添加至系統服務中:
E:\webdev\mongodb>mongod --logpath e:\webdev\mongodb\logs\mongodb.log --logappen
d --dbpath e:\webdev\mongodb\data --directoryperdb --serviceName MongoDB --install
然後訪問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);