mongo常見的一些進階操作
阿新 • • 發佈:2020-11-02
摘自:微信公眾號:無量測試之道
1、MongoDB高階操作
資料是隨意構建的,大家在練習時可以自己建立,現有的資料為:
> db.test.find()
{ "_id" : ObjectId("5eecc55ec2e3725b5715931b"), "name" : "tony", "age" : 33, "job" : "tester" }
{ "_id" : ObjectId("5eecc5a3c2e3725b5715931c"), "name" : "tom", "age" : 29, "job" : "developer" }
{ "_id" : ObjectId("5eeccc7b613abf5d90d640a2"
), "name" : "kim", "age" : 39, "job" : "developer" }{ "_id" : ObjectId("5eeccd88613abf5d90d640a3"), "name" : "jim", "age" : "39", "job" : "developer" }
{ "_id" : ObjectId("5ef74025613abf5d90d640a4"), "name" : "jeffy", "age" : 40, "job" : "developer" }
{ "_id" : ObjectId("5ef74035613abf5d90d640a5"), "name" : "jeee"
, "age" : 35, "job" : "developer" }{ "_id" : ObjectId("5ef7534a613abf5d90d640a6"), "name" : "jeee", "age" : 35, "job" : [ "developer", "tester" ] }
{ "_id" : ObjectId("5ef75351613abf5d90d640a7"), "name" : "jeee", "age" : 35, "job" : [ "developer", "pm" ] }
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name"
: "jeee", "age" : 35, "job" : [ "tester", "pm" ] }{ "_id" : ObjectId("5ef756d2613abf5d90d640a9"), "name" : { "last" : "jeee", "first" : "tony" }, "age" : 35, "job" : [ "tester", "pm" ] }
{ "_id" : ObjectId("5ef75e54613abf5d90d640aa"), "results" : [ { "item" : "a", "qty" : 26, "tags" : [ "blank", "red" ], "dim_cm" : [ 1, 10 ] }, { "item" : "a", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 15, 30 ] }, { "item" : "a", "qty" : 28, "tags" : [ "blank", "red" ], "dim_cm" : [ 50, 70 ] }, { "item" : "b", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 80, 90 ] } ] }
2、$all匹配所有
> db.test.find({job: {$all : ["pm", "tester"]}})// 對陣列的查詢, 欄位job中,既包含"pm",又包含"tester"的紀錄
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }
3、$size陣列元素個數
> db.test.find({"job" : {"$size" : 2}}) // 對陣列的查詢, 查詢陣列元素個數是3的記錄,$size前面無法和其他的操作符複合使用
{ "_id" : ObjectId("5ef7534a613abf5d90d640a6"), "name" : "jeee", "age" : 35, "job" : [ "developer", "tester" ] }
{ "_id" : ObjectId("5ef75351613abf5d90d640a7"), "name" : "jeee", "age" : 35, "job" : [ "developer", "pm" ] }
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }
4、$exists判斷欄位是否存在
> db.test.find({age: {$exists: false}}) #查詢不存在age 欄位的記錄,如果為true 表示存在
5、$regex正則表示式匹配
> db.test.find({name: {$regex: /^t.*/}}) #查詢name是以t 開頭的記錄
{ "_id" : ObjectId("5eecc55ec2e3725b5715931b"), "name" : "tony", "age" : 33, "job" : "tester" }
{ "_id" : ObjectId("5eecc5a3c2e3725b5715931c"), "name" : "tom", "age" : 29, "job" : "developer" }
6、巢狀查詢
> db.test.find({"name.last":"jeee"})
{ "_id" : ObjectId("5ef756d2613abf5d90d640a9"), "name" : { "last" : "jeee", "first" : "tony" }, "age" : 35, "job" : [ "tester", "pm" ] }
7、聚合查詢操作aggregate() 之sum求和
> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$sum: "$age"}}}])
{ "_id" : "tony", "num_tutorial" : 33 }
{ "_id" : "tom", "num_tutorial" : 29 }
{ "_id" : "jeffy", "num_tutorial" : 40 }
{ "_id" : "jeee", "num_tutorial" : 140 }
{ "_id" : { "last" : "jeee", "first" : "tony" }, "num_tutorial" : 35 }
{ "_id" : "kim", "num_tutorial" : 39 }
{ "_id" : "jim", "num_tutorial" : 0 }
相當於mysql 的
select name, count(*) from test group by name
8、聚合查詢操作aggregate() 之max求最大值
$group : {_id: "$name",num_tutorial : {$max: "$age"}}}]) db.test.aggregate([{
9、聚合查詢操作aggregate() 之min求最小值
$group : {_id: "$name",num_tutorial : {$min: "$age"}}}]) db.test.aggregate([{
10、聚合查詢操作aggregate() 之avg求平均值
$group : {_id: "$name",num_tutorial : {$min: "$age"}}}]) db.test.aggregate([{
11、$elemMatch內嵌文件查詢匹配
> db.test.find({},{ results: { $elemMatch: { "item":"b","qty":27}},_id:0})
{ "results" : [ { "item" : "b", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 80, 90 ] } ] }
Tips:
-
陣列中元素要是內嵌文件。
-
如果多個元素匹配$elemMatch條件,操作符返回陣列中第一個匹配條件的元素。
12、$where 的妙用
> db.test.find({$where: "this.age > 35"})
{ "_id" : ObjectId("5eeccc7b613abf5d90d640a2"), "name" : "kim", "age" : 39, "job" : "developer" }
{ "_id" : ObjectId("5eeccd88613abf5d90d640a3"), "name" : "jim", "age" : "39", "job" : "developer" }
{ "_id" : ObjectId("5ef74025613abf5d90d640a4"), "name" : "jeffy", "age" : 40, "
1、MongoDB高階操作
資料是隨意構建的,大家在練習時可以自己建立,現有的資料為:
> db.test.find()
{ "_id" : ObjectId("5eecc55ec2e3725b5715931b"), "name" : "tony", "age" : 33, "job" : "tester" }
{ "_id" : ObjectId("5eecc5a3c2e3725b5715931c"), "name" : "tom", "age" : 29, "job" : "developer" }
{ "_id" : ObjectId("5eeccc7b613abf5d90d640a2"), "name" : "kim", "age" : 39, "job" : "developer" }
{ "_id" : ObjectId("5eeccd88613abf5d90d640a3"), "name" : "jim", "age" : "39", "job" : "developer" }
{ "_id" : ObjectId("5ef74025613abf5d90d640a4"), "name" : "jeffy", "age" : 40, "job" : "developer" }
{ "_id" : ObjectId("5ef74035613abf5d90d640a5"), "name" : "jeee", "age" : 35, "job" : "developer" }
{ "_id" : ObjectId("5ef7534a613abf5d90d640a6"), "name" : "jeee", "age" : 35, "job" : [ "developer", "tester" ] }
{ "_id" : ObjectId("5ef75351613abf5d90d640a7"), "name" : "jeee", "age" : 35, "job" : [ "developer", "pm" ] }
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }
{ "_id" : ObjectId("5ef756d2613abf5d90d640a9"), "name" : { "last" : "jeee", "first" : "tony" }, "age" : 35, "job" : [ "tester", "pm" ] }
{ "_id" : ObjectId("5ef75e54613abf5d90d640aa"), "results" : [ { "item" : "a", "qty" : 26, "tags" : [ "blank", "red" ], "dim_cm" : [ 1, 10 ] }, { "item" : "a", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 15, 30 ] }, { "item" : "a", "qty" : 28, "tags" : [ "blank", "red" ], "dim_cm" : [ 50, 70 ] }, { "item" : "b", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 80, 90 ] } ] }
2、$all匹配所有
> db.test.find({job: {$all : ["pm", "tester"]}})// 對陣列的查詢, 欄位job中,既包含"pm",又包含"tester"的紀錄
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }
3、$size陣列元素個數
> db.test.find({"job" : {"$size" : 2}}) // 對陣列的查詢, 查詢陣列元素個數是3的記錄,$size前面無法和其他的操作符複合使用
{ "_id" : ObjectId("5ef7534a613abf5d90d640a6"), "name" : "jeee", "age" : 35, "job" : [ "developer", "tester" ] }
{ "_id" : ObjectId("5ef75351613abf5d90d640a7"), "name" : "jeee", "age" : 35, "job" : [ "developer", "pm" ] }
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }
4、$exists判斷欄位是否存在
> db.test.find({age: {$exists: false}}) #查詢不存在age 欄位的記錄,如果為true 表示存在
5、$regex正則表示式匹配
> db.test.find({name: {$regex: /^t.*/}}) #查詢name是以t 開頭的記錄
{ "_id" : ObjectId("5eecc55ec2e3725b5715931b"), "name" : "tony", "age" : 33, "job" : "tester" }
{ "_id" : ObjectId("5eecc5a3c2e3725b5715931c"), "name" : "tom", "age" : 29, "job" : "developer" }
6、巢狀查詢
> db.test.find({"name.last":"jeee"})
{ "_id" : ObjectId("5ef756d2613abf5d90d640a9"), "name" : { "last" : "jeee", "first" : "tony" }, "age" : 35, "job" : [ "tester", "pm" ] }
7、聚合查詢操作aggregate() 之sum求和
> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$sum: "$age"}}}])
{ "_id" : "tony", "num_tutorial" : 33 }
{ "_id" : "tom", "num_tutorial" : 29 }
{ "_id" : "jeffy", "num_tutorial" : 40 }
{ "_id" : "jeee", "num_tutorial" : 140 }
{ "_id" : { "last" : "jeee", "first" : "tony" }, "num_tutorial" : 35 }
{ "_id" : "kim", "num_tutorial" : 39 }
{ "_id" : "jim", "num_tutorial" : 0 }
相當於mysql 的
select name, count(*) from test group by name
8、聚合查詢操作aggregate() 之max求最大值
$group : {_id: "$name",num_tutorial : {$max: "$age"}}}]) db.test.aggregate([{
9、聚合查詢操作aggregate() 之min求最小值
$group : {_id: "$name",num_tutorial : {$min: "$age"}}}]) db.test.aggregate([{
10、聚合查詢操作aggregate() 之avg求平均值
$group : {_id: "$name",num_tutorial : {$min: "$age"}}}]) db.test.aggregate([{
11、$elemMatch內嵌文件查詢匹配
> db.test.find({},{ results: { $elemMatch: { "item":"b","qty":27}},_id:0})
{ "results" : [ { "item" : "b", "qty" : 27, "tags" : [ "blank", "red" ], "dim_cm" : [ 80, 90 ] } ] }
Tips:
-
陣列中元素要是內嵌文件。
-
如果多個元素匹配$elemMatch條件,操作符返回陣列中第一個匹配條件的元素。
12、$where 的妙用
> db.test.find({$where: "this.age > 35"})
{ "_id" : ObjectId("5eeccc7b613abf5d90d640a2"), "name" : "kim", "age" : 39, "job" : "developer" }
{ "_id" : ObjectId("5eeccd88613abf5d90d640a3"), "name" : "jim", "age" : "39", "job" : "developer" }
{ "_id" : ObjectId("5ef74025613abf5d90d640a4"), "name" : "jeffy", "age" : 40, "job" : "developer" }