C# and mongodb

來源:互聯網
上載者:User

首先從網上下載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);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.