MongoDB--"Detailed and specific operations of document query

Table of contents

D

Paginated List Query

Sort query

Regular query with complex conditions

compare query

contains query

conditional join query


D

Statistics query uses the count() method, and its syntax format is as follows:

db.collection.count(query,options)
Parameter Type Description
query document query selection criteria
options document Optional, extra options for modifying the count

Next, I add the following data to my empty collection myTables, and the explanations of the article are all around these data:

db.myTables.insertMany([
	{"_id":"1","name":"张三","age":"18","gender":"男"},
	{"_id":"2","name":"李四","age":"28","gender":"男"},
	{"_id":"3","name":"王五","age":"38","gender":"女"},
	{"_id":"4","name":"陈六","age":"48","gender":"男"},
	{"_id":"5","name":"胡七","age":"58","gender":"女"}
])

Use count  to count all records . By default, the count() method returns the number of all records that meet the conditions, as follows:

Count the number of records by condition , as follows:

Paginated List Query

You can use the limit() method to read the specified amount of data, and use the skip() method to skip the data of the specified data. Basic format:

db.collection.find().limit(number).skip(number)

If you want to return a specified number of records, you can call limit() after the find() method to return the result. For example, if you want to view the first and second data, as follows:

If we want to view the third and fourth pieces of data, we need to look beyond the first and second, as follows:

Sort query

The sort() method sorts the data. The sort() method can specify the sorting field through parameters, and use 1 and -1 to specify the sorting method, where 1 is for ascending sorting, and -1 is for descending sorting. The syntax is as follows:

db.collection.find().sort({KEY:1})
db.集合名称.find().sort(排序方式)

For example, descending order by age is as follows:

Regular query with complex conditions

MongoDB's fuzzy query is implemented through regular expressions. Regular expressions are the grammar of js, and their format is:

db.collection.find({field:/正则表达式/})
db.集合名称.find({字段:/正则表达式/})

For example, I want to query the data named Wang Wu, as follows:

For example, I want to match any data whose age ends with 8, as follows:

compare query

Comparison queries operate by using < , <= , > , >= symbols, and their basic format is as follows:

db.集合名称.find( {"field" : {$gt : value}} ) // 大于:field > value
db.集合名称.find( {"field" : {$lt : value}} ) // 小于:field < value
db.集合名称.find( {"field" : {$gte : value}} ) // 大于等于:field >= value
db.集合名称.find( {"field" : {$lte : value}} ) // 小于等于:field <= value
db.集合名称.find( {"field" : {$ne : value}} ) // 不等于:field != value

 Example: Query the data of people whose age is greater than or equal to 20:

contains query

Contains using the $in operator, as follows:

Does not include the use of the $nin operator, as follows:

conditional join query

If we need the query to meet more than two conditions, we need to use the $and operator to associate the conditions (equivalent to and in SQL). Its format is: $and: [{},{},{}], examples are as follows:

If there is an OR relationship between two or more conditions, we use operators to associate them. The format is the same as that of the previous and: For example:

Summary :

Query all data: db.collection.find();

Conditional query data: db.collection.find({condition})
Query the first record that meets the condition: db.collection.findOne({condition})
Query the first few records that meet the condition: db.collection.find({condition }).limit(number of items)

Query the skipped records that meet the conditions: db.collection.find({condition}).skip(number of items)
modify data: db.collection.update({condition},{modified data}) or db.collection. update({condition},{$set:{field to be modified: data})
modify data and auto-increment a field value: db.collection.update({condition},{$inc:{autoincrement field: step into value}})

Delete data: db.collection.remove({condition})
statistical query: db.collection.count({condition})
fuzzy query: db.collection.find({field name:/regular expression/})

Conditional comparison operation: db.collection.find({field name:{$gt:value]})
contains query: db.collection.find({field name:{$in:[value1,value2]}}) or db.collection.find({field name:{$nin:[value 1, value 2]}])

Conditional join query: db.collection.find({$and: [{condition 1},{condition 2]]}) or db.collection.find({$or :[{condition 1},{condition 2}]} )

Guess you like

Origin blog.csdn.net/qq_53123067/article/details/129011365