MongoDB introduction practice part 2

Welcome to pay attention to the official account of the road to unlimited testing, and reply to get resources,
Python Programming learning resources
UI automation of Python+Appium framework APP
UI automation of Python+Selenium framework Web
Python+Unittest framework API automation

Free resources and codes~
There is a official account QR code at the bottom of the article, which can be directly scanned through wechat.

 

1. MongoDB advanced operations

The data is constructed randomly. You can create it yourself during the exercise. The existing data are:

> 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 matches all

> db.test.find({job: {$all : ["pm", "tester"]}})// For the query of array, the field job contains"pm",Also contains"tester"Record of
{ "_id" : ObjectId("5ef7535d613abf5d90d640a8"), "name" : "jeee", "age" : 35, "job" : [ "tester", "pm" ] }

3. $size number of array elements

> db.test.find({"job" : {"$size" : 2}}) // Queries on arrays, Query the records with 3 elements in the array, $size The preceding cannot be combined with other operators
{ "_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 judge whether the field exists

> db.test.find({age: {$exists: false}}) #Query does not exist age Field, if true Indicates presence

5. $regex regular expression matching

 
> db.test.find({name: {$regex: /^t.*/}}) #query name So t Records at the beginning
{ "_id" : ObjectId("5eecc55ec2e3725b5715931b"), "name" : "tony", "age" : 33, "job" : "tester" }
{ "_id" : ObjectId("5eecc5a3c2e3725b5715931c"), "name" : "tom", "age" : 29, "job" : "developer" }

6. Nested query

> db.test.find({"name.last":"jeee"})
{ "_id" : ObjectId("5ef756d2613abf5d90d640a9"), "name" : { "last" : "jeee", "first" : "tony" }, "age" : 35, "job" : [ "tester", "pm" ] }

7. sum of aggregate query operation aggregate()

> 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 }
Equivalent to mysql of
select name, count(*) from test group by name

8. max of aggregate query operation aggregate()

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

9. min minimum of aggregate query operation aggregate()

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

10. avg averaging of aggregate query operation aggregate()

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

11. $elemMatch inline document query matching

> 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. If the elements in the array are embedded documents.

  2. If multiple elements match the $elemMatch condition, the operator returns the first element in the array that matches the condition.

12. How $where works

 
> 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" }

Note: my personal official account has been officially opened and is dedicated to sharing testing technologies, including big data testing, function testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc. wechat search official account: "the way of unlimited testing", or scan the QR code below:

Add attention and grow together.

Posted by webfandango on Wed, 01 Jun 2022 12:28:54 +0530