<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
事前準備:插入如下資料
db.Students.insert([ { _id:1, name:"Zhao", age:25, country:"USA", books:["JS","C++","EXTJS","MONGODB"]}, { _id:2, name:"Qian",age:22, country:"USA", books:["PHP","JAVA","EXTJS","C++"]}, { _id:3,name:"Sun",age:26, country:"USA", books:["JS","JAVA","C#","MONGODB"]}, { _id:4, name:"Li",age:27,country:"China",books:["JS","JAVA","EXTJS","MONGODB"]}, { _id:5,name:"Zhou", age:30,country:"China",books:["JS","C#","PHP","MONGODB"]}, { _id:6, name:"Wu", age:27, country:"Japan", books:["JS","JAVA","C++","MONGODB"]}, { _id:7, name:"Zheng", age:27, country:"UK", books:["JS","JAVA","EXTJS","PHP"]}, { _id:8, name:"Wang", age:26, country:"Korea",books:["JS","C#","EXTJS","MONGODB"]} ])
db.[檔名].find ({條件},{鍵指定})
查詢出所有資料的指定鍵(name ,age ,country)
db.Students.find({},{name:1,age:1,country:1,_id:0})
比較操作符 | 意義 | 舉例 |
---|---|---|
$lt | < | 查詢出id小於5的學生 > db.Students.find({_id:{$lt:5}},{}) |
$lte | <= | 查詢出年齡小於等於25歲之間的學生 > db.Students.find({age:{$lte:25}},{}) |
$ne | != | 查詢出國家不是中國的學生 > db.Students.find({country:{$ne:"China"}},{}) |
$gt | > | 查詢所有年紀大於27歲的,中國學生名字 > db.Students.find({age:{$gt:27}},{name:1,country:1,age:1}) { "_id" : 5, "name" : "Zhou", "age" : 30, "country" : "China" } |
$gte | >= | 同上 |
較操作符 | 意義 | 舉例 |
---|---|---|
$in | 包含 | 查詢國家是中國和美國的學生 > db.Students.find({country:{$in:["China","USA"]}},{}) |
$nin | 不包含 | 查詢年齡不是27歲的學生 > db.Students.find({age:{$nin:[27]}},{}) |
較操作符 | 意義 | 舉例 |
---|---|---|
$or | 包含 | 查詢年齡小於27歲,或者國家是美國的學生 >db.Students.find({$or:[{age:{$lt:27}},{country:"USA"}]},{}) 查詢年齡大於等於30歲,或者國家是不是美國的學生 >db.Students.find({$or:[{age:{$gte:30}},{country:{$nin:["China"]}}]},{}) |
為所有美國學生新增性別屬性為男性(M)
> db.Students.update({country:"USA"},{$set:{sex:"M"}},false,true)
查詢所有sex屬性為null的學生
> db.Students.find({sex:{$in:[null]}},{name:1,country:1})
查詢出名字中存在”Zh”的學生的資訊
> db.Students.find({name:/Zh/},{}) { "_id" : 1, "name" : "Zhao", "age" : 25, "country" : "USA", "books" : [ "JS", "C++", "EXTJS", "MONGODB" ], "sex" : "M" } { "_id" : 5, "name" : "Zhou", "age" : 30, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] } { "_id" : 7, "name" : "Zheng", "age" : 27, "country" : "UK", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
※$not和$nin的區別是$not可以用在任何地方兒$nin是用到集合上的
查詢出名字中不存在“Zh”的學生資訊
> db.Students.find({name:{$not:/Zh/}},{}) { "_id" : 2, "name" : "Qian", "age" : 22, "country" : "USA", "books" : [ "PHP","JAVA", "EXTJS", "C++" ], "sex" : "M" } { "_id" : 3, "name" : "Sun", "age" : 26, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ], "sex" : "M" } { "_id" : 4, "name" : "Li", "age" : 27, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ] } { "_id" : 6, "name" : "Wu", "age" : 27, "country" : "Japan", "books" : [ "JS", "JAVA", "C++", "MONGODB" ] } { "_id" : 8, "name" : "Wang", "age" : 26, "country" : "Korea", "books" : [ "JS","C#", "EXTJS", "MONGODB" ] }
查詢所有擁有JS和PHP書籍的同學
> db.Students.find({books:{$all:["JS","PHP"]}},{}) { "_id" : 5, "name" : "Zhou", "age" : 30, "country" : "China", "books" : [ "JS", "C#", "PHP", "MONGODB" ] } { "_id" : 7, "name" : "Zheng", "age" : 27, "country" : "UK", "books" : [ "JS", "JAVA", "EXTJS", "PHP" ] }
查詢第三本書是C#的同學
> db.Students.find({"books.2":"C#"},{}) { "_id" : 3, "name" : "Sun", "age" : 26, "country" : "USA", "books" : [ "JS", "JAVA", "C#", "MONGODB" ], "sex" : "M" }
上面那個使用index來查詢的例子中,"books.2"一定要用""包含起來
它不能與比較查詢符一起使用(這是弊端)
插入一條book陣列有兩本數的同學
> db.Students.insert({_id:9,name:"Xu",age:26,country:"Japan",books:["C#","PHP"]}) WriteResult({ "nInserted" : 1 })
查詢只有兩本書的同學
> db.Students.find({books:{$size:2}},{}) { "_id" : 9, "name" : "Xu", "age" : 26, "country" : "Japan", "books" : [ "C#", "PHP" ] }
查詢名字是“Li”的喜歡的書的數量
> var person = db.Students.find({name:"Li"}) > while(person.hasNext()){ obj = person.next(); print(obj.books.length) }
查詢名字為“Wang”書架中第1~3本書
> db.Students.find({name:"Wang"},{books:{$slice:[0,3]}}) { "_id" : 8, "name" : "Wang", "age" : 26, "country" : "Korea", "books" : [ "JS", "C#", "EXTJS" ] }
查詢出最後一本書
> db.Students.find({name:"Wang"},{books:{$slice:-1}}) { "_id" : 8, "name" : "Wang", "age" : 26, "country" : "Korea", "books" : [ "MONGODB" ] }
新增一個物件陣列到“Li”同學,記錄“Li”同學的成績
> var li = [{ ... subject :"Math", ... score: 90 ... },{ ... subject :"English", ... score:85 ... },{ ... subject :"History", ... score:95 ... }] > db.Students.update({name:"Li"},{$set:{school:li}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.Students.find({name:"Li"},{}) { "_id" : 4, "name" : "Li", "age" : 27, "country" : "China", "books" : [ "JS", "JAVA", "EXTJS", "MONGODB" ], "school" : [{ "subject" : "Math", "score" : 90 },{ "subject" : "English", "score" : 85 }, { "subject" : "History", "score" : 95 }] } >
查詢參加了數學考試,並且分數為90的同學
> db.Students.find({school:{subject:"Math",score:90}},{_id:0,name:1}) { "name" : "Li" }
但是問題存在如下:
條件順序變化時候,
> db.Students.find({school:{score:90,subject:"Math"}},{_id:0,name:1}) > --查不到東西--
條件數目不一致的時候,也同樣查不到
> db.Students.find({"school.subject":"Math","school.score":90},{name:1}) { "_id" : 4, "name" : "Li" }
這種方式支援順序的變化,但是也同樣存在問題,那就是匹配的問題,條件不是作為一對條件來進行匹配的
例如:
> db.Students.find({"school.subject":"Math","school.score":85},{name:1}) { "_id" : 4, "name" : "Li" }
這裡的85分是英語成績
> db.Students.find({school:{$elemMatch:{subject:"Math",score:90}}},{name:1}) { "_id" : 4, "name" : "Li" } > db.Students.find({school:{$elemMatch:{score:90,subject:"Math"}}},{name:1}) { "_id" : 4, "name" : "Li" } > db.Students.find({school:{$elemMatch:{subject:"Math"}}},{name:1}) { "_id" : 4, "name" : "Li" } >
查詢出Student檔案中前5條資料
> db.Students.find().limit(5)
查詢出persons檔案中3~8條的資料
> db.Students.find().limit(5).skip(2)
查詢所有資料,按照年齡排序
正序
> db.Students.find({},{_id:0,age:1,name:1}).sort({age:1}) { "name" : "Qian", "age" : 22 } { "name" : "Zhao", "age" : 25 } { "name" : "Sun", "age" : 26 } { "name" : "Wang", "age" : 26 } { "name" : "Xu", "age" : 26 } { "name" : "Wu", "age" : 27 } { "name" : "Zheng", "age" : 27 } { "name" : "Li", "age" : 27 } { "name" : "Zhou", "age" : 30 }
倒序
> db.Students.find({},{_id:0,age:1,name:1}).sort({age:-1}) { "name" : "Zhou", "age" : 30 } { "name" : "Wu", "age" : 27 } { "name" : "Zheng", "age" : 27 } { "name" : "Li", "age" : 27 } { "name" : "Sun", "age" : 26 } { "name" : "Wang", "age" : 26 } { "name" : "Xu", "age" : 26 } { "name" : "Zhao", "age" : 25 } { "name" : "Qian", "age" : 22 } >
skip效能不好,可以採用插入時間的做法來彌補,具體方法如下:
var persons = db.persons.find(); while(persons.hasNext()){ obj = persons.next(); print(obj.name) }
使用者端發來資訊叫他銷燬
遊標迭代完畢
預設遊標超過10分鐘沒用也會別清除
快照後就會針對不變的集合進行遊標運動了,看看使用方法.
db.persons.find({$query:{name:」Jim」},$snapshot:true})
為什麼用快照,以為MongoDB在進行更新的時候,例如新增一些鍵值對,那麼MongoDB的處理不會在原來的索引位置上進行更新操作,而是會把
更新之後的資料,放在末尾,那麼就導致了前後兩次進行查詢時候相同索引對應不同資料的情況
補充:
高階查詢選項
到此這篇關於MongoDB實現查詢操作的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45