.NET連接MongoDB數(shù)據(jù)庫實例教程
使用代碼
讓我們從Mongo數(shù)據(jù)庫的一些細節(jié)和基本命令開始,并最終介紹如何創(chuàng)建一個可連接至Mongo數(shù)據(jù)庫的.NET Windows應(yīng)用。
Mongo數(shù)據(jù)庫
MongoDB 是一個跨平臺、文檔導(dǎo)向的數(shù)據(jù)庫系統(tǒng),它被歸類為“NoSQL”數(shù)據(jù)庫。MongoDB避開了傳統(tǒng)的基于表的關(guān)系數(shù)據(jù)庫結(jié)構(gòu),而是使用了帶動態(tài)模式的類JSON文檔。MongoDB將這種格式稱為BSON(二進制JSON)。這種動態(tài)模式使得特定類型應(yīng)用中的數(shù)據(jù)整合更簡單、更快速。MongoDB是自由且開源的軟件。
Mongo數(shù)據(jù)庫的特性
Ad hoc 查詢
1 標引
2 復(fù)制
3 負載均衡
4 文件存貯
5 聚合
6 服務(wù)器端 JavaScript 執(zhí)行
7 定容集合
用戶可從 此處 下載Mongo數(shù)據(jù)庫,然后將其中內(nèi)容解壓至任一文件夾。 文件下載完成后,用戶需要配置MongoDB的數(shù)據(jù)文件夾。做法是在“C:\Data”文件夾下創(chuàng)建一個名為“DB”的文件夾。
數(shù)據(jù)文件夾創(chuàng)建好以后,可以通過用命令行提示符在“bin”文件夾下運行“mongod.exe”來啟動Mongo數(shù)據(jù)庫。
現(xiàn)在數(shù)據(jù)庫已經(jīng)啟動,并正在運行。
創(chuàng)建一個.NET應(yīng)用
創(chuàng)建一個.NET web/Windows應(yīng)用。在這個示例中,我們將用一個簡單的員工表。
開始之前,我們需要確保系統(tǒng)中裝有MongoDB的.NET驅(qū)動。你可以按下面的步驟來為一個指定的項目安裝驅(qū)動。
打開Visual Studio的包管理器:
打開包管理器控制臺后,用戶可以執(zhí)行下面的命令:
Install-Package mongocsharpdriver
在項目中添加對下列命名空間的引用:
using MongoDB.Bson;
using MongoDB.Driver;
//此外,你將頻繁的用到下面這些 using 語句中的一條或多條:
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;

聲明數(shù)據(jù)庫服務(wù)器和數(shù)據(jù)庫的變量:
MongoServer _server;
MongoDatabase _database;
用下面的命令連接至數(shù)據(jù)庫。在這里,數(shù)據(jù)庫服務(wù)器是在本地主機上運行的,端口為:27017,數(shù)據(jù)庫名為“ anoop”。
private void Form1_Load(object sender, EventArgs e)
{
string connection = "mongodb://localhost:27017";
_server = MongoServer.Create(connection);
_database = _server.GetDatabase("anoop", SafeMode.True);
}
在這里,我們創(chuàng)建了三個使用不同屬性集合的類。我們可以設(shè)置這些類的屬性,并將數(shù)據(jù)保存至同一個數(shù)據(jù)庫、同一個表。這是無模式數(shù)據(jù)庫的真正優(yōu)勢:插入數(shù)據(jù)時不檢查模式。保存不同的記錄時可以用不同的域的集合,而其它的域 將 默認被視為NULL。
public class Users1
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class Users2
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
public class Users3
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Location { get; set; }
}
private void rbEntity1_CheckedChanged(object sender, EventArgs e)
{
txtName.Enabled = true;
txtAge.Enabled = true;
txtLocation.Enabled = true;
}
private void rbEntity2_CheckedChanged(object sender, EventArgs e)
{
txtName.Enabled = true;
txtAge.Enabled = false;
txtLocation.Enabled = true;
}
private void rbEntity3_CheckedChanged(object sender, EventArgs e)
{
txtName.Enabled = true;
txtAge.Enabled = true;
txtLocation.Enabled = false;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (rbEntity1.Checked)
{
var _users = _database.GetCollection<users3 />("users");
var user = new Users3 { };
user.Age = Convert.ToInt32(txtAge.Text);
user.Name = txtName.Text;
user.Location = txtLocation.Text;
_users.Insert(user);
var id = user.Id;
}
else if (rbEntity2.Checked)
{
var _users = _database.GetCollection<users2 />("users");
var user = new Users2 { };
user.Name = txtName.Text;
user.Location = txtLocation.Text;
_users.Insert(user);
var id = user.Id;
}
else if (rbEntity3.Checked)
{
var _users = _database.GetCollection<users1 />("users");
var user = new Users1 { };
user.Age = Convert.ToInt32(txtAge.Text);
user.Name = txtName.Text;
_users.Insert(user);
var id = user.Id;
}
MessageBox.Show("User with name " + txtName.Text + " created");
}
///下面的代碼幫助你從Mongo數(shù)據(jù)庫中查找一條現(xiàn)有記錄。
_collection = _database.GetCollection<users1 />("users");
IMongoQuery query = Query.EQ("Name", "Anoop");
Users1 _user = _collection.FindAs<users1 />(query).FirstOrDefault();
MessageBox.Show(_user.Age.ToString());
///下面的代碼幫助你更新Mongo數(shù)據(jù)庫中的一條現(xiàn)有記錄。
_collection = _database.GetCollection<users1 />("users");
IMongoQuery query = Query.EQ("Name", "Anoop");
Users1 _user = _collection.FindAs<users1 />(query).FirstOrDefault();
MessageBox.Show("Age before update :" + _user.Age.ToString());
//更新年齡的值
_user.Age = 30;
//保存更改
_collection.Save(_user);
MessageBox.Show("Age after update :" + _user.Age.ToString());
相關(guān)文章
C#中的文件路徑獲取函數(shù)和文件名字獲取函數(shù)小結(jié)
這篇文章主要介紹了C#中的文件路徑獲取函數(shù)和文件名字獲取函數(shù)小結(jié),本文講解了獲取絕對文件路徑、獲取文件名字、獲得包含 path 目錄信等內(nèi)容,需要的朋友可以參考下2015-01-01用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法
本篇文章小編為大家介紹,用NPOI創(chuàng)建Excel、合并單元格、設(shè)置單元格樣式、邊框的方法。需要的朋友參考下2013-04-04Unity3D獲取當前鍵盤按鍵及Unity3D鼠標、鍵盤的基本操作
這篇文章主要介紹了Unity3D獲取當前鍵盤按鍵及Unity3D鼠標、鍵盤的基本操作的相關(guān)資料,需要的朋友可以參考下2015-11-11Unity的IPostprocessBuildWithReport實用案例深入解析
這篇文章主要為大家介紹了Unity的IPostprocessBuildWithReport實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05