首先添加所需要驅(qū)動包(可通過nuget獲得)
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
一、設(shè)置配置信息
//
連接信息
static
string
conn =
"
mongodb://localhost
"
;
static
string
database =
"
test
"
;
static
string
collection =
"
person
"
;
static
MongoServer mongodb = MongoServer.Create(conn);
//
連接數(shù)據(jù)庫
static
MongoDatabase db = mongodb.GetDatabase(database);
//
選擇數(shù)據(jù)庫名
static
MongoCollection mc = db.GetCollection(collection);
//
選擇集合,相當于表
二、插入數(shù)據(jù)庫
public
static
void
Insert(Person p)
{
mc.Insert(p);
}
public
static
void
Insert(BsonDocument b)
{
mc.Insert(b);
}
三、更新數(shù)據(jù)庫
public
static
void
Update(Person p)
{
BsonDocument bd
=
BsonExtensionMethods.ToBsonDocument(p);
IMongoQuery query
= Query.EQ(
"
_id
"
, p._id);
mc.Update(query,
new
UpdateDocument(bd));
}
public
static
void
Update(QueryDocument q,UpdateDocument u)
{
mc.Update(q, u);
}
?
四、刪除某條記錄
public
static
void
Delete(
int
id)
{
mc.Remove(Query.EQ(
"
_id
"
, id));
}
五、查詢數(shù)據(jù)庫
public
static
Person Find(
int
id)
{
return
mc.FindOneAs<Person>(Query.EQ(
"
_id
"
, id));
}
public
static
MongoCursor<Person>
FindAll()
{
return
mc.FindAllAs<Person>
();
}
public
static
MongoCursor<Person>
Select(QueryDocument q)
{
return
mc.FindAs<Person>
(q);
}
六、統(tǒng)計數(shù)據(jù)個數(shù)
public
static
long
Count(QueryDocument q)
{
return
mc.Count(q);
}
七、排序和分頁
public
static
MongoCursor<Person> SkipAndLimit(
int
a,
int
b)
{
return
mc.FindAllAs<Person>
().SetSkip(a).SetLimit(b);
}
?
八、應(yīng)用與示例
static
void
Main(
string
[] args)
{
mongodb.Connect();
//
var person = Find(124);
//
Console.WriteLine(person.Name);
//
person.Name = "guizhu";
//
Update(person);
//
Console.WriteLine(person.Name);
var
query =
new
QueryDocument { {
"
_id
"
,
13
} };
var
update =
new
UpdateDocument { {
"
$set
"
,
new
QueryDocument { {
"
PassWord
"
,
"
aaaaa
"
} } } };
Update(query,update);
//
對象插入
//
Person p = new Person { _id = 12, Name = "hello", PassWord = "4444" };
//
Insert(p);
//
BsonDocument 插入
//
BsonDocument b = new BsonDocument();
//
b.Add("_id", 13);
//
b.Add("Name", "world");
//
b.Add("PassWord", "6666");
//
Insert(b);
//
var p= FindAll();
//
foreach (var person in p)
//
{
//
Console.WriteLine(person.Name);
//
}
//
QueryDocument query = new QueryDocument();
//
BsonDocument b = new BsonDocument();
//
b.Add("$gte", 123);
//
b.Add("$lt", 125);
//
query.Add("_id", b);
//
var p=Select(query);
//
foreach (var person in p)
//
{
//
Console.WriteLine(person.Name);
//
}
//
Console.WriteLine(Count(query));
//
FieldsDocument f = new FieldsDocument();
//
f.Add("Name", 1);
//
MongoCursor<Person> m = mc.FindAs<Person>(query).SetFields(f);
//
foreach (var person in p)
//
{
//
Console.WriteLine(person.Name);
//
}
//
SortByDocument s = new SortByDocument();
//
s.Add("_id", 1);
//
-1=DESC
//
var p = Sort(s);
//
foreach (var person in p)
//
{
//
Console.WriteLine(person.Name);
//
}
//
var p = SkipAndLimit(1, 3);
//
foreach (var person in p)
//
{
//
Console.WriteLine(person.Name);
//
}
//
Delete(12);
Console.WriteLine(
"
完成
"
);
Console.ReadLine();
}
?
class
Person
{
public
int
_id;
public
string
Name;
public
string
PassWord;
}
源碼下載地址:鏈接:http://pan.baidu.com/s/1c0tlXZi 密碼:1es5
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

