MongoDB query advanced operation details (multi condition query regular matching query etc.)


Advanced operations for MongoDB queries

Grammar is introduced

MongoDB queries documents using the find() method, while the find() method displays all the queried documents unstructured.

-- 1. The basic grammar
db.collection.find(query, projection) --  Returns all documents that match the query criteria
db.collection.findOne(query, projection) --  Returns the first 1 There are three documents that meet the query criteria
-- query : Optional, query condition operator, used to specify a query condition
-- projection : Optional, drop drop operator, which specifies the key to return (omitted by default)
--  case 1 Query: users The age in the set is zero 18 All documents of the
db.users.find({age: 18})

-- 2. If you need to view the data in a readable way, use pretty() methods
db.collection.find(query, projection).pretty()

AND conditions and OR conditions

The find() method of MongoDB can pass multiple keys, each separated by a comma, thus fulfilling the AND condition of SQL

-- 1.AND Conditional basic grammar
db.collection.find({key1:value1, key2:value2})
--  case 1 Query: users The age in the set is zero 18 All documentation for girls
db.users.find({age: 18, sex: 'girl'})

-- 2.OR Conditional basic grammar
db.collection.find({
 $or: [
 {key1: value1},
 {key2:value2}
 ]
})
--  case 2 Query: users The age in the set is zero 18 Or all documents whose gender is female
db.users.find({
 $or: [
 {age: 18},
 {sex: 'girl'}
 ]
})

Conditional operator

The conditional operator processes the conditional relationship to retrieve the qualified document data from MongoDB. The conditional operator is as follows:

More than: $gt Less than: $lt > or equal to: $gte Less than or equal to: $lte

--  The query users The age in the set is greater than 18 The age of the document data
db.users.find({age : {$gt : 18}})
--  The query users The age in the set is less than 18 The age of the document data
db.users.find({age : {$lt : 18}})
--  The query users The age in the set is greater than or equal to 18 The age of the document data
db.users.find({age : {$gte : 18}})
--  The query users The age in the set is greater than or equal to 18 The age of the document data
db.users.find({age : {$lte : 18}})

$type operators

The $type operator retrieves the matching data types in the collection based on the BSON type. The MongoDB operator retrieves the matching data types in the collection based on the BSON type.

类型$type代表数字说明
Double164位浮点数
String2字符串类型
Object3对象类型
Array4数组类型
Binary Data52进制数据类型
Objectid7对象id类型
Boolean8布尔类型
Date9日期类型
Null10用于表示空值或不存在的字段
Regular Expression11正则表达式类型
JavaScript13JavaScript代码
JavaScript (with scope)15带作用域的JavaScript代码
32-bit integer1632位整数
Timestamp17时间戳类型
64-bit integer1864位整数
Min key-1最小键
Max key127最大键

Here I will use $type as an example of the query condition:

--  Example: query users Collection of documents whose names are of type string
db.users.find({"name" : {$type : 2}})

The ES49en (), ES50en (), ES51en () methods are used in the query

The limit() method reads a specified number of document data records based on the query criteria. The skip() method skips a specified amount of document data and then queries according to the query criteria. The sort() method sorts the queried document data in ascending or descending order according to the specified field. A field value of 1 refers to ascending order, and a field value of -1 refers to descending order. I’ll use the limit() and skip() methods for examples below.

--  Use grammar ( limit() , skip() , sort() Methods can be used in combination.
db.collectionName.find().limit(NUMBER)
db.collectionName.find().skip(NUMBER)
db.collectionName.find().sort({"key": 1/-1})
--  case 1 Before: skip 50 Bar data query users The name field in the collection is of type string 100 Data within
db.users.find({"name" : {$type : 2}}).limit(100).skip(50)
--  case 2 : will be queried users The document data in the collection whose name field is of type string is sorted in ascending order
db.users.find({"name" : {$type : 2}}).sort({"name": 1})

Use regular expressions in queries

Here’s a look at regular expressions: a regular expression is a string that describes and matches a series of strings that conform to a certain syntax rule.

MongoDB uses the $regex operator to set the regular expression language for matching strings.

--  Use the syntax
db.collectionName.find({key:{ $regex: regex, $options: options }})
--  Example: case-insensitive query users The name in the collection contains web Document data of ( The query results are the same in the following two ways )
db.users.find({ "name" : { $regex : "web", $options: "i" } })
db.users.find({ "name" : /web/i } })

conclusion