主頁(yè) > 知識(shí)庫(kù) > MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼

MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼

熱門(mén)標(biāo)簽:智能電銷(xiāo)機(jī)器人銷(xiāo)售話術(shù) 機(jī)器人外呼系統(tǒng)軟件存在問(wèn)題 福州電銷(xiāo)機(jī)器人源代碼 高德地圖標(biāo)注商戶位置 兗州電話外呼營(yíng)銷(xiāo)系統(tǒng) 徐州ai電銷(xiāo)機(jī)器人原理 沈陽(yáng)營(yíng)銷(xiāo)電銷(xiāo)機(jī)器人招商 企業(yè)智能外呼系統(tǒng)價(jià)格多少 南京400電話怎樣辦理

MongoDB簡(jiǎn)介

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

MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。

下面給大家介紹MongoDB查詢文檔操作的實(shí)例

先把student刪除,再重新插入數(shù)據(jù)

> db.student.drop()
true
> db.student.insert([{ "_id" : 1, "name" : "zhangsan", "age": 27, "sex": 1 }, { "_id" : 2, "name" : "lisi", "age": 27 } ,{ "_id" : 3, "name" : "wangwu", "age": 30 }, { "_id" : 4, "name" : "zhaoliu", "age": 28 }, { "_id" : 5, "name" : "qianliu", "age": 33 }, { "_id" : 6, "name" : "sunba", "age": 32 }])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

1、查詢指定鍵

db.集合名稱.find({查詢條件},{指定鍵})

指定鍵:1表示顯示,0表示不顯示,_id默認(rèn)顯示

> db.student.find({},{name:1})
{ "_id" : 1, "name" : "zhangsan" }
{ "_id" : 2, "name" : "lisi" }
{ "_id" : 3, "name" : "wangwu" }
{ "_id" : 4, "name" : "zhaoliu" }
{ "_id" : 5, "name" : "qianliu" }
{ "_id" : 6, "name" : "sunba" }
> db.student.find({},{_id:0, age:0})
{ "name" : "zhangsan", "sex" : 1 }
{ "name" : "lisi" }
{ "name" : "wangwu" }
{ "name" : "zhaoliu" }
{ "name" : "qianliu" }
{ "name" : "sunba" }
> db.student.find({},{_id:0, name:1})
{ "name" : "zhangsan" }
{ "name" : "lisi" }
{ "name" : "wangwu" }
{ "name" : "zhaoliu" }
{ "name" : "qianliu" }
{ "name" : "sunba" } 

2、各種查詢方式

$lt:
$lte:=
$gt:>
$gte:>=
$ne:!=
> db.student.find({age:{$lt:30}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
> db.student.find({age:{$ne:27}})
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$in:包含

$nin:不包含

> db.student.find({age:{$in:[27,28]}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
> db.student.find({age:{$nin:[27,28]}})
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$or:或者

> db.student.find({$or:[{age:{$lt:29}}, {name:"sunba"}]})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

null:空值

> db.student.find({sex: null})
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 } 

$type:鍵是某種類型的

double:1
string:2
...

> db.student.insert({_id:7, name:7, age:70})
WriteResult({ "nInserted" : 1 })
> db.student.find({name: {$type: 2}})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
> db.student.find({name: {$type: 1}})
{ "_id" : 7, "name" : 7, "age" : 70 } 

正則表達(dá)式

> db.student.find({name: /si\b/})
{ "_id" : 2, "name" : "lisi", "age" : 27 } 

db.集合名稱.findOne({查詢條件},{指定鍵})
查詢出符合條件的第一條數(shù)據(jù)

> db.student.findOne()
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 } 

db.集合名稱.find({查詢條件},{指定鍵}).limit(數(shù)字)
查詢前幾條數(shù)據(jù)

> db.student.find().limit(3)
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 } 

db.集合名稱.find({查詢條件},{指定鍵}).skip(數(shù)字)
跳過(guò)前幾條數(shù)據(jù)

> db.student.find().skip(2)
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 7, "name" : 7, "age" : 70 } 

可以使用limit()和skip()實(shí)現(xiàn)分頁(yè)

> db.student.find().skip(0).limit(3)
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
> db.student.find().skip(3).limit(3)
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
> db.student.find().skip(6).limit(3)
{ "_id" : 7, "name" : 7, "age" : 70 } 

db.集合名稱.find().sort({鍵:數(shù)字})
數(shù)字為1表示升序,數(shù)字為2表示降序

> db.student.find().sort({age:1})
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 7, "name" : 7, "age" : 70 }
> db.student.find().sort({age:1, _id:-1})
{ "_id" : 2, "name" : "lisi", "age" : 27 }
{ "_id" : 1, "name" : "zhangsan", "age" : 27, "sex" : 1 }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 }
{ "_id" : 3, "name" : "wangwu", "age" : 30 }
{ "_id" : 6, "name" : "sunba", "age" : 32 }
{ "_id" : 5, "name" : "qianliu", "age" : 33 }
{ "_id" : 7, "name" : 7, "age" : 70 } 

以上內(nèi)容是小編給大家介紹的MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼,希望對(duì)大家有所幫助!

您可能感興趣的文章:
  • PHP庫(kù) 查詢Mongodb中的文檔ID的方法
  • MongoDB中對(duì)文檔的增刪查改基本操作方法總結(jié)
  • MongoDB快速入門(mén)筆記(三)之MongoDB插入文檔操作
  • MongoDB快速入門(mén)筆記(六)之MongoDB的文檔修改操作
  • MongoDB快速入門(mén)筆記(六)之MongoDB刪除文檔操作
  • MongoDB修改、刪除文檔的域?qū)傩詫?shí)例
  • MongoDB學(xué)習(xí)筆記(四) 用MongoDB的文檔結(jié)構(gòu)描述數(shù)據(jù)關(guān)系
  • MongoDB插入、更新、刪除文檔實(shí)現(xiàn)代碼

標(biāo)簽:丹東 鶴崗 邯鄲 景德鎮(zhèn) 本溪 大理 吉安 昭通

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼》,本文關(guān)鍵詞  MongoDB,快速,入門(mén),筆記,四,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MongoDB快速入門(mén)筆記(四)之MongoDB查詢文檔操作實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章