溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

發(fā)布時(shí)間:2021-11-03 09:39:40 來源:億速云 閱讀:126 作者:柒染 欄目:大數(shù)據(jù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)MongoDB的本質(zhì)及怎么進(jìn)行安裝配置,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

如果你從來沒有接觸MongoDB或?qū)ongoDB有一點(diǎn)了解,如果你是C#開發(fā)人員,那么你不妨花幾分鐘看看本文。

一,簡介

   MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。由C++語言編寫。旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。

   MongoDB是一個(gè)高性能,開源,無模式的文檔型數(shù)據(jù)庫,是當(dāng)前NoSql數(shù)據(jù)庫中比較熱門的一種。

    MongoDB是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點(diǎn)是他支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。

   傳統(tǒng)的關(guān)系數(shù)據(jù)庫一般由數(shù)據(jù)庫(database)、表(table)、記錄(record)三個(gè)層次概念組成,MongoDB是由數(shù)據(jù)庫(database)、集合(collection)、文檔對象(document)三個(gè)層次組成。MongoDB對于關(guān)系型數(shù)據(jù)庫里的表,但是集合中沒有列、行和關(guān)系概念,這體現(xiàn)了模式自由的特點(diǎn)。

二,特點(diǎn)

  它的特點(diǎn)是高性能、易部署、易使用,存儲(chǔ)數(shù)據(jù)非常方便。主要功能特性有:

  1)面向集合存儲(chǔ),易存儲(chǔ)對象類型的數(shù)據(jù)。

  2)模式自由。

  3)支持動(dòng)態(tài)查詢。

  4)支持完全索引,包含內(nèi)部對象。

  5)支持查詢。

  6)支持復(fù)制和故障恢復(fù)。

  7)使用高效的二進(jìn)制數(shù)據(jù)存儲(chǔ),包括大型對象(如視頻等)。

  8)自動(dòng)處理碎片,以支持云計(jì)算層次的擴(kuò)展性。

  9)支持RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。

  10)文件存儲(chǔ)格式為BSON(一種JSON的擴(kuò)展)。

  11)可通過網(wǎng)絡(luò)訪問。

三,下載安裝和開啟服務(wù)器

3.1)MongoDB 當(dāng)前版本是2.0.4,下載地址:http://www.mongodb.org/downloads。提供了各種平臺(tái)的版本。我這里選擇的是Windows平臺(tái)下的。

3.2)新建目錄E:\ mongodb , 將下載的壓縮包解壓到此目錄。bin文件夾下有一堆.exe 文件

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

其中有兩個(gè)最重要的文件:Mongod.exe和Mongo.exe 。

Mongod.exe 是用來連接到mongo數(shù)據(jù)庫服務(wù)器的,即服務(wù)器端。

Mongo.exe 是用來啟動(dòng)MongoDB shell的,即客戶端。

其他文件:

mongodump 邏輯備份工具。

mongorestore 邏輯恢復(fù)工具。

mongoexport  數(shù)據(jù)導(dǎo)出工具。

mongoimport  數(shù)據(jù)導(dǎo)入工具。

3.3)開啟服務(wù)器

第一步:新建一個(gè)目錄用來存放MongoDB的數(shù)據(jù)庫文件,即dbpath。隨便建在那都可以,我這里建在 E:\MongoDBFiles 。 這是為了下一步使用的。

第二步:打開CMD窗口,鍵入如下命令

> e:

> cd e:\mongodb\mongodb-win32-i386-2.0.4\bin

> mongod.exe -dbpath "E:\mongodbfiles"

最后一行命令中的-dbpath 參數(shù)值就是我們第一步新建的文件夾。這個(gè)文件夾一定要在開啟服務(wù)之前事先建立好,否則會(huì)報(bào)錯(cuò),mongodb不會(huì)自己創(chuàng)建。

如果操作成功會(huì)出現(xiàn)如下界面:

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

該界面該我們展示了一些信息:如進(jìn)程ID是2988,端口號是27017。

打開瀏覽器輸入:http://127.0.0.1:27017/

我們看到了這樣的提示:

“You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number”

到此,MongoDB數(shù)據(jù)庫服務(wù)已經(jīng)成功啟動(dòng)了。

四,使用mongo.exe 執(zhí)行數(shù)據(jù)庫增刪改查操作

mongodb 為我們提供的客戶端管理工具是mongo.exe

4.1)創(chuàng)建數(shù)據(jù)庫:

雙擊打開mongo.exe 出現(xiàn)如下界面:

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

該界面的意思是,當(dāng)前連接的數(shù)據(jù)庫是test,這是系統(tǒng)默認(rèn)將要?jiǎng)?chuàng)建的。為什么說是“將要?jiǎng)?chuàng)建的”呢?因?yàn)榇藭r(shí)并不存在此數(shù)據(jù)庫,或者說它現(xiàn)在還只在內(nèi)存中,并沒有創(chuàng)建在物理磁盤上。不信,你看MongoDBFiles文件夾下面除了mongod.lock外,什么都沒有。只有當(dāng)你執(zhí)行了插入數(shù)據(jù)的命令后,該數(shù)據(jù)庫才會(huì)真正的創(chuàng)建。

好了,我們暫時(shí)不管這個(gè)test了?,F(xiàn)在我們來創(chuàng)建一個(gè)叫cnblogs 的數(shù)據(jù)庫。

在shell 命令窗口鍵入如下命令:

> use cnblogs // use 命令用來切換當(dāng)前數(shù)據(jù)庫,如果該數(shù)據(jù)庫不存在,則會(huì)先新建一個(gè)。

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

4.2)創(chuàng)建collection并插入數(shù)據(jù)

在傳統(tǒng)關(guān)系型數(shù)據(jù)庫中,創(chuàng)建完了庫后接下來會(huì)創(chuàng)建表,但是在mongoDB中沒有“表”的概念,與其對應(yīng)的一個(gè)概念是集合,即collection。

在shell 命令窗口鍵入如下命令:

> db.users.insert({'name':'xumingxiang','sex':'man'})

// 這條命令是向users 集合中插入一條數(shù)據(jù)。如果集合users不存在,則會(huì)先新建一個(gè),然后再插入數(shù)據(jù),參數(shù)以JSON格式傳入。

因?yàn)槲覀兒竺嬉獪y試刪除數(shù)據(jù),所以我們再插入一條數(shù)據(jù):

> db.users.insert({'name':xiangshu','sex':'man'})

4.3)在上面4.1)和4.2)我們創(chuàng)建了數(shù)據(jù)庫,創(chuàng)建了集合,還插入了兩條數(shù)據(jù),那么這些操作有沒有執(zhí)行成功呢?我們來查詢一下:

在shell 命令窗口鍵入如下命令:

> show dbs // 顯示所有數(shù)據(jù)庫

>show collections // 顯示當(dāng)前數(shù)據(jù)庫下的所有集合

>db.users.find() // 顯示users集合下的所有數(shù)據(jù)文檔

shell 界面如下:

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

看我用紅色標(biāo)記的部分。這說明我們之前的操作是成功的。我們還看到系統(tǒng)給每條記錄分配了一個(gè)惟一主鍵 _id 。

4.4)更新數(shù)據(jù)

現(xiàn)在我們要把第二條數(shù)據(jù)的sex改成女即“women”

在shell 命令窗口鍵入如下命令:

> db.users.update({'name':'xiangshu'},{'$set':{'sex':'women'}},upsert=true,multi=false)

解釋一下幾個(gè)參數(shù):

第一:查詢的條件

第二:更新的字段

第三:如果不存在則插入

第四:是否允許修改多條記錄

4.5)刪除記錄

我們現(xiàn)在要把第一條記錄即'name'為'xumingxiang'的

在shell 命令窗口鍵入如下命令:

> db. users.remove({'name':'xumingxiang'})

我們在檢驗(yàn)一下4)5)兩步有沒有操作成功,在shell 命令窗口鍵入如下命令:

> db.users.find()

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

從輸出的界面我們看到現(xiàn)在只剩下一條'name'為'xiangshu'的了,并且它的'sex'為'women',這說明4)5)兩步操作成功了。

4.6)刪除所有記錄

> db.users.remove()

4.7) 刪除collection

> db.users.drop() //如果刪除成功會(huì)返回“true”,否則返回“false”

4.8)刪除當(dāng)前數(shù)據(jù)庫

> db.dropDatabase()

五,更多命令

db.AddUser(username,password)  添加用戶

db.auth(usrename,password)     設(shè)置數(shù)據(jù)庫連接驗(yàn)證

db.cloneDataBase(fromhost)     從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫

db.commandHelp(name)           returns the help for the command

db.copyDatabase(fromdb,todb,fromhost)  復(fù)制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標(biāo)數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務(wù)器地址

db.createCollection(name,{size:3333,capped:333,max:88888})  創(chuàng)建一個(gè)數(shù)據(jù)集,相當(dāng)于一個(gè)表

db.currentOp()                 取消當(dāng)前庫的當(dāng)前操作

db.dropDataBase()              刪除當(dāng)前數(shù)據(jù)庫

db.eval(func,args)             run code server-side

db.getCollection(cname)        取得一個(gè)數(shù)據(jù)集合,同用法:db['cname'] or

db.getCollenctionNames()       取得所有數(shù)據(jù)集合的名稱列表

db.getLastError()              返回最后一個(gè)錯(cuò)誤的提示消息

db.getLastErrorObj()           返回最后一個(gè)錯(cuò)誤的對象

db.getMongo()                  取得當(dāng)前服務(wù)器的連接對象get the server

db.getMondo().setSlaveOk()     allow this connection to read from then nonmaster membr of a replica pair

db.getName()                   返回當(dāng)操作數(shù)據(jù)庫的名稱

db.getPrevError()              返回上一個(gè)錯(cuò)誤對象

db.getProfilingLevel()        

db.getReplicationInfo()        獲得重復(fù)的數(shù)據(jù)

db.getSisterDB(name)           get the db at the same server as this onew

db.killOp()                    停止(殺死)在當(dāng)前庫的當(dāng)前操作

db.printCollectionStats()      返回當(dāng)前庫的數(shù)據(jù)集狀態(tài)

db.printReplicationInfo()

db.printSlaveReplicationInfo()

db.printShardingStatus()       返回當(dāng)前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫

db.removeUser(username)        刪除用戶

db.repairDatabase()            修復(fù)當(dāng)前數(shù)據(jù)庫

db.resetError()                

db.runCommand(cmdObj)          run a database command. if cmdObj is a string, turns it into {cmdObj:1}

db.setProfilingLevel(level)    0=off,1=slow,2=all

db.shutdownServer()            關(guān)閉當(dāng)前服務(wù)程序

db.version()                   返回當(dāng)前程序的版本信息

db.test.find({id:10})          返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集

db.test.find({id:10}).count()  返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)

db.test.find({id:10}).limit(2) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集

db.test.find({id:10}).skip(8)  返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集

db.test.find({id:10}).limit(2).skip(8)  返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)

db.test.find({id:10}).sort()   返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集

db.test.findOne([query])       返回符合條件的一條數(shù)據(jù)

db.test.getDB()                返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱

db.test.getIndexes()           返回些數(shù)據(jù)集的索引信息

db.test.group({key:...,initial:...,reduce:...[,cond:...]})

db.test.mapReduce(mayFunction,reduceFunction,<optional params>)

db.test.remove(query)                      在數(shù)據(jù)集中刪除一條數(shù)據(jù)

db.test.renameCollection(newName)          重命名些數(shù)據(jù)集名稱

db.test.save(obj)                          往數(shù)據(jù)集中插入一條數(shù)據(jù)

db.test.stats()                            返回此數(shù)據(jù)集的狀態(tài)

db.test.storageSize()                      返回此數(shù)據(jù)集的存儲(chǔ)大小

db.test.totalIndexSize()                   返回此數(shù)據(jù)集的索引文件大小

db.test.totalSize()                        返回些數(shù)據(jù)集的總大小

db.test.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)

db.test.validate()                         驗(yàn)證此數(shù)據(jù)集

db.test.getShardVersion()                  返回?cái)?shù)據(jù)集共享版本號

六,MongoDB語法與現(xiàn)有關(guān)系型數(shù)據(jù)庫SQL語法比較

MongoDB語法                                   MySql語法

db.test.find({'name':'foobar'}) <==> select * from test where name='foobar'

db.test.find()                  <==> select * from test

db.test.find({'ID':10}).count() <==> select count(*) from test where ID=10

db.test.find().skip(10).limit(20)     <==> select * from test limit 10,20

db.test.find({'ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)

db.test.find().sort({'ID':-1})        <==> select * from test order by ID desc

db.test.distinct('name',{'ID':{$lt:20}})  <==> select distinct(name) from test where ID<20

db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})  <==> select name,sum(marks) from test group by name

db.test.find('this.ID<20',{name:1})  <==> select name from test where ID<20

db.test.insert({'name':'foobar','age':25})<==>insert into test ('name','age') values('foobar',25)

db.test.remove({})                <==> delete * from test

db.test.remove({'age':20})        <==> delete test where age=20

db.test.remove({'age':{$lt:20}})  <==> elete test where age<20

db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20

db.test.remove({'age':{$gt:20}})  <==> delete test where age>20

db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20

db.test.remove({'age':{$ne:20}})  <==> delete test where age!=20

db.test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}})  <==> update test set age=age+3 where name='foobar'

注意以上命令大小寫敏感

七,可視化的客戶端管理工具M(jìn)ongoVUE

使用mongo.exe 管理數(shù)據(jù)庫雖然可行,功能也挺強(qiáng)大,但每次都要敲命令,即繁瑣枯燥而且效率低下。下面介紹一款Windows下的可視化操作的管理工具M(jìn)ongoVUE

下載地址:http://www.mongovue.com/downloads/

運(yùn)行效果如下:

MongoDB的本質(zhì)及怎么進(jìn)行安裝配置

八,在C#中使用官方驅(qū)動(dòng)操作MongoDB

8.1)下載安裝

想要在C#中使用MongoDB,首先得要有個(gè)MongoDB支持的C#版的驅(qū)動(dòng)。C#版的驅(qū)動(dòng)有很多種,如官方提供的,samus。 實(shí)現(xiàn)思路大都類似。這里我們先用官方提供的mongo-csharp-driver ,當(dāng)前版本為1.4.1

下載地址:http://github.com/mongodb/mongo-csharp-driver/downloads

編譯之后得到兩個(gè)dll

MongoDB.Driver.dll:顧名思義,驅(qū)動(dòng)程序

MongoDB.Bson.dll:序列化、Json相關(guān)

然后在我們的程序中引用這兩個(gè)dll。

下面的部分簡單演示了怎樣使用C#對MongoDB進(jìn)行增刪改查操作。

8.2)連接數(shù)據(jù)庫:

在連接數(shù)據(jù)庫之前請先確認(rèn)您的MongoDB已經(jīng)開啟了。

//數(shù)據(jù)庫連接字符串

const string strconn = "mongodb://127.0.0.1:27017";

//數(shù)據(jù)庫名稱

const string dbName = "cnblogs";

//創(chuàng)建數(shù)據(jù)庫鏈接

MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);

//獲得數(shù)據(jù)庫cnblogs

MongoDatabase db = server.GetDatabase(dbName);

8.3)插入數(shù)據(jù):

好了數(shù)據(jù)打開了,現(xiàn)在得添加數(shù)據(jù)了,我們要添加一條User“記錄”到 Users集合中。

在MongoDB中沒有表的概念,所以在插入數(shù)據(jù)之前不需要?jiǎng)?chuàng)建表。

但我們得定義好要插入的數(shù)據(jù)的模型Users

Users.cs:

public class Users

{

    public ObjectId _id;//BsonType.ObjectId 這個(gè)對應(yīng)了 MongoDB.Bson.ObjectId

 public string Name { get; set; } 

    public string Sex { set; get; }

}

_id 屬性必須要有,否則在更新數(shù)據(jù)時(shí)會(huì)報(bào)錯(cuò):“Element '_id' does not match any field or property of class”。

好,現(xiàn)在看看添加數(shù)據(jù)的代碼怎么寫:

public void Insert()

{

    //創(chuàng)建數(shù)據(jù)庫鏈接

    MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);

    //獲得數(shù)據(jù)庫cnblogs

    MongoDatabase db = server.GetDatabase(dbName);

    Users users = new Users();

    users.Name = "xumingxiang";

    users.Sex = "man";

    //獲得Users集合,如果數(shù)據(jù)庫中沒有,先新建一個(gè)

    MongoCollection col = db.GetCollection("Users");

    //執(zhí)行插入操作

    col.Insert<Users>(users);

}

8.4)更新數(shù)據(jù)

public void Update()

{

    //創(chuàng)建數(shù)據(jù)庫鏈接

    MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);

    //獲得數(shù)據(jù)庫cnblogs

    MongoDatabase db = server.GetDatabase(dbName);

    //獲取Users集合

    MongoCollection col = db.GetCollection("Users");

    //定義獲取“Name”值為“xumingxiang”的查詢條件

    var query = new QueryDocument { { "Name", "xumingxiang" } };

    //定義更新文檔

    var update = new UpdateDocument { { "$set", new QueryDocument { { "Sex", "wowen" } } } };

    //執(zhí)行更新操作

    col.Update(query, update);

}

8.5)刪除數(shù)據(jù)

public void Delete()

{

    //創(chuàng)建數(shù)據(jù)庫鏈接

    MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);

    //獲得數(shù)據(jù)庫cnblogs

    MongoDatabase db = server.GetDatabase(dbName);

    //獲取Users集合

    MongoCollection col = db.GetCollection("Users");

    //定義獲取“Name”值為“xumingxiang”的查詢條件

    var query = new QueryDocument { { "Name", "xumingxiang" } };

    //執(zhí)行刪除操作

    col.Remove(query);

}

8.6)查詢數(shù)據(jù)

public void Query()

{

    //創(chuàng)建數(shù)據(jù)庫鏈接

    MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);

    //獲得數(shù)據(jù)庫cnblogs

    MongoDatabase db = server.GetDatabase(dbName);

    //獲取Users集合

    MongoCollection col = db.GetCollection("Users");

    //定義獲取“Name”值為“xumingxiang”的查詢條件

    var query = new QueryDocument { { "Name", "xumingxiang" } };

    //查詢?nèi)考侠锏臄?shù)據(jù)

    var result1 = col.FindAllAs<Users>();

    //查詢指定查詢條件的第一條數(shù)據(jù),查詢條件可缺省。

    var result2 = col.FindOneAs<Users>();

    //查詢指定查詢條件的全部數(shù)據(jù)

    var result3 = col.FindAs<Users>(query);

}

九,在C#中使用samus驅(qū)動(dòng)操作MongoDB

再來介紹一款第三方驅(qū)動(dòng)samus,這是一款使用使用較多的驅(qū)動(dòng),更新頻率比較快,samus驅(qū)動(dòng)除了支持一般形式的操作之外,還支持Linq 和Lambda 表達(dá)式。

下載地址:https://github.com/samus/mongodb-csharp

下載回來編譯得到兩個(gè)dll

MongoDB.dll          驅(qū)動(dòng)的主要程序

MongoDB.GridFS.dll    用于存儲(chǔ)大文件。

這里我們引用MongoDB.dll  即可。關(guān)于MongoDB.GridFS.dll 本文用不到,暫不介紹,無視它。

其連接數(shù)據(jù)庫以及CRUD操作如下:

//數(shù)據(jù)庫連接字符串

const string strconn = "mongodb://127.0.0.1:27017";

//數(shù)據(jù)庫名稱

const string dbName = "cnblogs";

//定義數(shù)據(jù)庫

MongoDatabase db;

/// <summary>

/// 打開數(shù)據(jù)庫鏈接

/// </summary>

public void GetConnection()

{

    //定義Mongo服務(wù)

    Mongo mongo = new Mongo(strconn);

    //打開連接

    mongo.Connect();

    //獲得數(shù)據(jù)庫cnblogs,若不存在則自動(dòng)創(chuàng)建

    db = mongo.GetDatabase(dbName) as MongoDatabase;

}

/// <summary>

/// 添加數(shù)據(jù)

/// </summary>

public void Insert()

{

    var col = db.GetCollection<Users>();

    //或者

    //var col = db.GetCollection("Users");

    Users users = new Users();

    users.Name = "xumingxiang";

    users.Sex = "man";

    col.Insert(users);

}

/// <summary>

/// 更新數(shù)據(jù)

/// </summary>

public void Update()

{

    var col = db.GetCollection<Users>();

    //查出Name值為xumingxiang的第一條記錄

    Users users = col.FindOne(x => x.Name == "xumingxiang");

    //或者

    //Users users = col.FindOne(new Document { { "Name", "xumingxiang" } });

    users.Sex = "women";

    col.Update(users, x => x.Sex == "man");

}

/// <summary>

/// 刪除數(shù)據(jù)

/// </summary>

public void Delete()

{

    var col = db.GetCollection<Users>();

    col.Remove(x => x.Sex == "man");

    ////或者

    ////查出Name值為xumingxiang的第一條記錄

    //Users users = col.FindOne(x => x.Sex == "man");

    //col.Remove(users);

}

/// <summary>

/// 查詢數(shù)據(jù)

/// </summary>

public void Query()

{

    var col = db.GetCollection<Users>();

    var query = new Document { { "Name", "xumingxiang" } };

    //查詢指定查詢條件的全部數(shù)據(jù)

    var result1 = col.Find(query);

    //查詢指定查詢條件的第一條數(shù)據(jù)

    var result2 = col.FindOne(query);

    //查詢?nèi)考侠锏臄?shù)據(jù)

    var result3 = col.FindAll();

}

十,寫個(gè)批處理,方便開啟Mongodb服務(wù)器

每次開啟Mongodb服務(wù)器都要打開CMD窗口,敲那么一段命令,反反復(fù)復(fù),你是不是也覺得煩呢?反正小弟本人厭惡敲dos命令,喜歡用鼠標(biāo)點(diǎn)點(diǎn)。

怎樣用鼠標(biāo)點(diǎn)一下就能開啟Mongodb服務(wù)器呢?可能你已經(jīng)想到了,寫個(gè)批處理程序不就搞定了嘛,是的,就是這樣,這個(gè)批處理很簡單。

全部代碼如下:

@echo

@pause

mongod -repair -dbpath "E:\mongodbfiles"

mongod -dbpath "E:\mongodbfiles"

@pause

注:“mongod -repair -dbpath "E:\mongodbfiles"”是為了解決啟動(dòng)時(shí)有時(shí)會(huì)報(bào)錯(cuò)“Unclean shutdown detected mongodb”。

把它拷貝到記事本,保存為.bat文件,然后和mongod.exe放在同一個(gè)目錄,雙擊它就OK了。

上述就是小編為大家分享的MongoDB的本質(zhì)及怎么進(jìn)行安裝配置了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI