C# and 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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM