mongo常见的一些进阶操作

摘自:微信公众号:无量测试之道

 

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求最大值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$max: "$age"}}}])

 

9、聚合查询操作aggregate() 之min求最小值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$min: "$age"}}}])

 

10、聚合查询操作aggregate() 之avg求平均值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$min: "$age"}}}])

 

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:

  1. 数组中元素要是内嵌文档。

  2. 如果多个元素匹配$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求最大值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$max: "$age"}}}])

 

9、聚合查询操作aggregate() 之min求最小值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$min: "$age"}}}])

 

10、聚合查询操作aggregate() 之avg求平均值

> db.test.aggregate([{$group : {_id: "$name",num_tutorial : {$min: "$age"}}}])

 

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:

  1. 数组中元素要是内嵌文档。

  2. 如果多个元素匹配$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" }
posted @ 2020-11-01 22:26  阿布_alone  阅读(125)  评论(0编辑  收藏  举报
TOP