1.進入mongodb安裝目錄(>cd D:\workPrograms\mongodb\bin)
2.>mongodb 啟動mongo db
3.>show dbs; 查詢所有數據庫
4.>use test; 選擇要查詢的數據庫,此處若test數據庫不存在會自動創建test數據庫
5.在users collection中插入6條用戶數據
>db.users.insertMany(
[
{
_id: 1,
name: "sue",
age: 19,
type: 1,
status: "P",
favorites: { artist: "Picasso", food: "pizza" },
finished: [ 17, 3 ],
badges: [ "blue", "black" ],
points: [
{ points: 85, bonus: 20 },
{ points: 85, bonus: 10 }
]
},
{
_id: 2,
name: "bob",
age: 42,
type: 1,
status: "A",
favorites: { artist: "Miro", food: "meringue" },
finished: [ 11, 25 ],
badges: [ "green" ],
points: [
{ points: 85, bonus: 20 },
{ points: 64, bonus: 12 }
]
},
{
_id: 3,
name: "ahn",
age: 22,
type: 2,
status: "A",
favorites: { artist: "Cassatt", food: "cake" },
finished: [ 6 ],
badges: [ "blue", "red" ],
points: [
{ points: 81, bonus: 8 },
{ points: 55, bonus: 20 }
]
},
{
_id: 4,
name: "xi",
age: 34,
type: 2,
status: "D",
favorites: { artist: "Chagall", food: "chocolate" },
finished: [ 5, 11 ],
badges: [ "red", "black" ],
points: [
{ points: 53, bonus: 15 },
{ points: 51, bonus: 15 }
]
},
{
_id: 5,
name: "xyz",
age: 23,
type: 2,
status: "D",
favorites: { artist: "Noguchi", food: "nougat" },
finished: [ 14, 6 ],
badges: [ "orange" ],
points: [
{ points: 71, bonus: 20 }
]
},
{
_id: 6,
name: "abc",
age: 43,
type: 1,
status: "A",
favorites: { food: "pizza", artist: "Picasso" },
finished: [ 18, 12 ],
badges: [ "black", "blue" ],
points: [
{ points: 78, bonus: 8 },
{ points: 57, bonus: 7 }
]
}
]
)
6.>db.users.find(); 查詢users里的所有數據
7.>db.users.find({status: 'A'}); 查詢users里status等於A的數據
8.>db.users.find({age:{$gt:22}}); 查詢users里年齡大於22的數據
9.> db.users.find({status: 'D', age: {$gt: 23}}); 查詢users里狀態為D並且年齡大於23的數據
10.>db.users.find({$or: [{status: 'A'}, {age: {$gt: 23}}]}); 查詢users里狀態為A或者年齡大於23的數據
11.>db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } ); 嵌套查詢,匹配整個子文檔,查詢favorites里artist為Picasso,food為pizza的用戶
12.>db.users.find( { "favorites.artist": "Picasso" } ); 嵌套查詢,匹配子文檔中的字段,通過點(.)符號來表示子文檔中的字段
13.>db.users.find( { badges: [ "blue", "black" ] } ); 數組查詢,匹配所有數組,查找badges的值為"['blue', 'black']"的用戶
14.>db.users.find( { badges: "black" } ); 數組查詢,查詢數組中的一個元素,查找badges的數組中包含了'black'元素的所有用戶
15.> db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } ); 數組多條件and查詢($elemMatch),$elemMatch多個條件and匹配,假設我們要找到滿足finished字段中的數組元素的值大於15並且小於20
結果:{ "_id" : 1, ..., "finished" : [ 17, 3 ] ,...}
{ "_id" : 6, ..., "finished" : [ 18, 12 ], ...}
16.>db.users.find( { finished: { $gt: 15, $lt: 20 } } ); 數組多條件or查詢,查詢大於15或者小於20的數據
結果:{ "_id" : 1,..., "finished" : [ 17, 3 ], ...}
{ "_id" : 2, ..., "finished" : [ 11, 25 ], ...}
{ "_id" : 6, ..., "finished" : [ 18, 12 ], ...}
17.>db.users.find( { 'points.0.points': { $lte: 55 } } ); 使用數組下標定位集合子文檔,查詢數組中第一個points的值,小於等於55的數據
18.>db.users.find( { 'points.points': { $lte: 55 } } ); 省略下標,查詢points數組中所有的points的值小於等於55的數據
19.> db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } ); 多條件and查詢($elemMatch),查詢points數組中points的值小於等於70並且bonus為20的數據
結果:{ "_id" : 3, ....,"points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }
20.>db.users.find({"points.points": {$lte: 70}, "points.bonus": 20}); 多條件or查詢, 查詢points數組中,points的值小於等於70或者bonus為20的數據
結果:{ "_id" : 2, ..., "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }
{ "_id" : 3, ..., "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }
參考資料地址:https://www.linuxidc.com/Linux/2016-10/136581.htm
