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

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

基本查詢

l 查詢的核心語法

|

db.infos.find(查詢條件,[,{設(shè)置顯示的字段}])

|

n db.infos.find()

n 查詢url為http://www.baidu.com

|

db.infos.find({url: “http://www.baidu.com”})

|

對于設(shè)置的顯示字段稱為數(shù)據(jù)的投影操作,如果不需要顯示的字段設(shè)置為0,如果需要顯示的字段設(shè)置為1;默認(rèn)值為1

n 不想顯示_id

|

db.infos.find(

{url: “http://www.baidu.com”}, {_id: 0}

)

|

n 如果想優(yōu)雅的顯示 .pretty()

|

db.infos.find(

{url: “http://www.baidu.com”}, {_id: 0}

).pretty()

|

n 查詢單個數(shù)據(jù)

|

db.infos.findOne(

{url: “http://www.baidu.com”}, {_id: 0}

).pretty()

|

關(guān)系運算

在mongodb中支持的關(guān)系查詢:$gt,$lt,$gte,$lte,$ne

準(zhǔn)備:

db.students.drop();

db.students.insert({name: “張三1”, age: 20, sex: “男”, score: 69, address: “海淀區(qū)”});

db.students.insert({name: “張三2”, age: 19, sex: “女”, score: 89, address: “海淀區(qū)”});

db.students.insert({name: “張三3”, age: 22, sex: “女”, score: 99, address: “海淀區(qū)”});

db.students.insert({name: “張三4” age: 23, sex: “男”, score: 83, address: “海淀區(qū)”});

db.students.insert({name: “張三5, age: 24, sex: “男”, score: 81, address: “海淀區(qū)”});

db.students.insert({name: “張三6”, age: 25, sex: “男”, score: 59, address: “海淀區(qū)”});

db.students.insert({name: “張三7”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”});

n 查詢姓名是張三的學(xué)生

|

db.students.find({name: “張三”}).pretty()

|

n 查詢性別是男的學(xué)生的信息

|

db.students.find({sex: “男”}).pretty()

|

n 查詢學(xué)生年齡大于20歲的學(xué)生

|

db.students.find(

 {

 age: {

 “$gt”: 20

}

}

).pretty()

|

n 查詢姓名不是張三2的信息

|

db.students.find(

 {

 name: {

 “$nt”: “張三2”

}

}

).pretty()

|

邏輯運算

主要有三種類型:$and,$or,$not|$nor

n 查詢學(xué)生年齡在20~22歲之間的

在進(jìn)行and連接是最容易的,只需要使用“,”分割若干個條件即可

|


db.students.find(

 {

 age: {“$gte”: 20, “$lte”22}

}

).pretty()

|

n 查詢學(xué)生年齡大于20歲,或成績大于90

|

db.students.find(

 {

 “$or”: [

 {age: { “$gt”: 20}},

{score: { “$gt”: 90}}

]

}

).pretty()

|

n 查詢學(xué)生年齡小于20歲,并且成績小于90的學(xué)生

|

db.students.find(

 {

  “$nor”: [

 {age: { “$gte”: 20}},

{score: { “$gte”: 90}}

]

}

).pretty()

|

模運算

模運算使用“$mod”來穩(wěn)層,語法

|

{$mod: [數(shù)字, 余數(shù)]}

|

n 例如:求對20取余,余數(shù)為2的學(xué)生

|

db.students.find(

 { “$age”: {“$mod”: [20,2]}}

)

|

范圍查詢

in 在范圍之中,nin不在范圍之中

n 查詢姓名是“張三”“張三2”的信息

|

db.students.find(

 {

“name”: {

*“$in”: [“張三”, “張三2”]

}

}

).pretty()

|

n 查詢姓名不是“張三”“張三2”的學(xué)生的信息

|


db.students.find(

 {

“name”: {

 “$nin”: [“張三”, “張三2”]

}

}

).pretty()

|

數(shù)組查詢

|

db.students.insert({name: “李四1”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”, course: [‘html’, ‘css’, ‘JavaScript’]});

db.students.insert({name: “李四2”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”, course: [‘html’, ‘css’, ‘JavaScript’, ‘jQuery’]));

db.students.insert({name: “李四3”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”, course: [‘html’, ‘css’, ‘JavaScript’, ‘jQuery’]});

db.students.insert({name: “李四4”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”, course: [‘html’, ‘css’, ‘JavaScript’, ‘jQuery’]));

db.students.insert({name: “李四5”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”, course: [‘html’, ‘css’, ‘JavaScript’, ‘jQuery’]));

|

數(shù)組數(shù)據(jù)進(jìn)行判斷,可以使用的運算符:$all,$size,$slice,$elemMatch

n 同時參加html與css課程的學(xué)生

u 現(xiàn)兩個數(shù)組內(nèi)容都需要保存,所以使用{“$all”: [內(nèi)容1,內(nèi)容2,……] }

|

db.students.find(

 {

 ‘course’: {

 “$all”: [‘html’, ‘css’]

}

}

).pretty();

|

n 查詢數(shù)組中第二個內(nèi)容(index = 3,下標(biāo)從0開始)為jQuery的信息

|

db.students.find(

 {“course.3”: “jQuery”}

).pretty()

|

n 查詢只參加兩門課程的學(xué)生

使用$size來控制數(shù)量

|

db.students.find(

 {

course: {

 ‘$size’: 2

}

}

).pretty()

|

n 返回所有學(xué)生年齡是19歲,但是只顯示兩門課程

目前只取前兩門的信息

|

db.students.find(

 {‘a(chǎn)ge’: 19},

 {

‘course’: {

 ‘$slice’: 2

}

}

).pretty()

|

n 返回所有學(xué)生年齡是19歲,但是顯示第2門與第3門的課程

$slice[index, num] 表示從index下標(biāo)開始數(shù),連續(xù)截取num位

|

db.students.find(

 {‘a(chǎn)ge’: 19},

 {

‘course’: {

 ‘$slice’:  [1,2]

}

}

).pretty()

|

嵌套集合

db.students.insert([

 {

 name: “王五”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”,parents: [

 {name: “王五-父親”, job: “局長”, age: 50},

 {name: “王五-目前”, job: “處長”, age: 48}

]

},

{

 name: “王五2”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”,parents: [

 {name: “王五2-父親”, job: “局長”, age: 50},

 {name: “王五2-目前”, job: “處長”, age: 48}

]

},

{

 name: “王五3”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”,parents: [

 {name: “王五3-父親”, job: “局長”, age: 50},

 {name: “王五3-目前”, job: “處長”, age: 48}

]

},

{

 name: “王五4”, age: 26, sex: “男”, score: 39, address: “海淀區(qū)”,parents: [

 {name: “王五4-父親”, job: “局長”, age: 50},

 {name: “王五4-目前”, job: “處長”, age: 48}

]

}

]);

n 查詢年齡大于20歲 ,父母職位有人是局長的信息

db.students.find(

 {

 "$and": [

 {'age': {"$gte": 20}},

 {'parents':

 {

 $elemMatch: {

 "job": '局長'

 }

 }

 }

 ]

 }

 ).pretty();

字段判斷是否存在

$exists 可以判斷某個字段是否存在,如果設(shè)置為true表示存在,如果設(shè)置為false則不存在

n 查詢具有parents的成員

|

db.students.find(

 {

 parents: {

 $exists: true

 }

 }

).pretty();

|

n 查詢地址不存在的信息

|

添加一個地址不存在的信息

db.students.insert(

 {

 name: "王五6", age: 26, sex: "男", score: 39, parents: [

 {name: "王五6-父親", job: "局長2", age: 50},

 {name: "王五6-目前", job: "處長", age: 48}

 ]

 }

)

查詢地址不存在的信息

db.students.find(

 {

 address: {

 $exists: false

 }

 }

).pretty()

|

where條件過濾

n 使用where進(jìn)行數(shù)據(jù)查詢

|

db.students.find(

 {

 $where: "this.age > 20"

 }

).pretty()

|
|

db.students.find(

 "this.age > 20"

).pretty() 

【不推薦】

|

對于where語句是可以簡化的,但是這類的操作屬于對于每一行都進(jìn)行比較,對于數(shù)據(jù)量較大的并不適合使用,實際上以上代碼就是編寫一個操作的函數(shù)

|

db.students.find(

 {

 $where: function(){

 return this.age > 20

 }

 }

).pretty()

|

db.students.find(

 function(){

 return this.age > 20

 }

).pretty()

|

以上只是查詢一個數(shù)據(jù),執(zhí)行了一個條件的判斷,如果多個條件需要使用$and連接

|

db.students.find(

 {

 $and: [

 {

 $where: "this.age > 20"

 },

 {

 $where: "this.age < 28"

 }

 ]

 }

).pretty()

|

雖然這種形式可以實現(xiàn)數(shù)據(jù)的查詢,但是其最大的缺點是在MongoDB保存的BSON數(shù)據(jù)重新變成了JavaScript的語法結(jié)構(gòu),這種方式不方便使用數(shù)據(jù)的索引機(jī)制,所以不建議使用

正則運算

如果需要使用模糊查詢,必須使用正則表達(dá)式,

格式:

l 基礎(chǔ)語法:{key: 正則標(biāo)記}

l 完整語法:{key:

{

$regex: 正則標(biāo)記,

$options: 選項

}

}

n 對于options主要設(shè)置正則信息查詢的標(biāo)記

u i :忽略大小寫

u m:多行查詢

u x:空白字符串,除了被轉(zhuǎn)義的或在字符類中意外的完全被忽略

u s:匹配所有字符,包含換行內(nèi)容

n 如果直接使用,那么只能使用i和m

n 查詢以王開頭的學(xué)生的信息

|

db.students.find(

 {

 name: /王/

 }

).pretty()

|

n 查詢數(shù)組數(shù)據(jù),查詢學(xué)生父母職位帶與校字段的信息

|


db.students.find(

 {

 parents: {

 $elemMatch: {

 job: /校/

 }

 }

 }

).pretty()

|

如果明白了,請點擊下方贊賞,1分也是愛。

聯(lián)系方式QQ:1718202383
可接應(yīng)屆畢業(yè)生論文項目
可接外包項目

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 任何數(shù)據(jù)庫中查詢都是最麻煩的,在MongoDB中對于查詢的支持非常到位,有關(guān)系運算,邏輯運算,數(shù)組運算等等首先對于...
    許先森的許閱讀 3,093評論 0 1
  • 只要是數(shù)據(jù)庫那么就絕對離不開最為核心的功能: CRUD, 所以在MongoDB里面對于數(shù)據(jù)的操作也是支持的, 但是...
    簡人CC閱讀 269評論 0 0
  • 目錄 查詢操作 集合查詢方法 find() 查詢內(nèi)嵌文檔 查詢操作符(內(nèi)含 數(shù)組查詢) "$gt" 、"$gte"...
    彩虹之夢閱讀 1,127評論 0 1
  • 只要是數(shù)據(jù)庫那么就絕對離不開最為核心的功能: C U R D ,所以在Mongo 里面對于數(shù)據(jù)的操作也是有支持的,...
    Mr_米飯閱讀 398評論 2 1
  • 一、MongoDB簡介 概述MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫,由C++語言編寫。旨在為WEB應(yīng)用提供...
    EndEvent閱讀 1,230評論 1 4

友情鏈接更多精彩內(nèi)容