What are the commonly used expressions in MongoDB database
This article introduces the knowledge of "what expressions are commonly used in MongoDB database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Aggregation (aggregate)
To put it simply, the result of the previous processing is handed over to the next processing, and the last one processes the output.
We call each process a pipeline.
The common pipes are:
$group: grouping for statistical results
$match: used to filter data
$project: modify structure, rename, add, delete fields, create calculation results, etc.
$sort: sort
$limit: number of documents displayed (how many rows of data are displayed)
$skip: how many documents are skipped before
$unwind: split the data type field
Common expression
$sum: summation
$avg: average
$min: get the minimum value
$max: get the maximum
$push: insert an array
$first: get the first document data
$last: get the last document data
Example:
# grouping by gender and calculating the number of people
Db.stu.aggregate (
{$group: {_ id: "$sex", count: {$sum:1}
)
Output:
{"_ id": "female", "count": 3}
{"_ id": "male", "count": 3}
# _ id specifies the field to be grouped, which needs to be written as $sex. $sum:1 indicates that this row of data is calculated as 1.
# calculate the average of different genders on the basis of the above
Db.stu.aggregate (
{$group: {_ id: "$sex", count: {$sum:1}, svg_age: {$avg:'$age'}}
)
Output:
{"_ id": "female", "count": 3, "agv_age": 22.6666666666668}
{"_ id": "male", "count": 3, "agv_age": 19.3333333333332}
# instead of grouping, calculate the average number and age of all
Db.stu.aggregate (
{$group: {_ id:null,count: {$sum:1}, svg_age: {$avg:'$age'}
)
# when grouping by gender and calculating the number of people, calculate the average value of different genders and only take the count value
# and rename count to sum, which is not realistic
Db.stu.aggregate (
{$group: {_ id:'$sex',count: {$sum:1}, agv_age: {$avg:'$age'}
{$project: {sum:'$count',_id:0}}
)
# _ id will be displayed by default. You need to give a 0, and if you don't write it, you won't show it.
Output:
{"sum": 3}
{"sum": 3}
# filter those with sum greater than 2 in the above example
Db.stu.aggregate (
{$group: {_ id:'$sex',count: {$sum:1}, agv_age: {$avg:'$age'}
{$project: {sum:'$count',_id:0}}
{$match: {sum: {$gt:2}
)
# sort
# in ascending order of age, descending order is-1
Db.stu.aggregate (
{$sort: {age:1}}
)
# $limit and $skip
# query two messages
Db.stu.aggregate (
{$limit:2}
)
# Skip the first two and show two
Db.stu.aggregate (
{$skip:2}
{$limit:2}
)
# $unwind
# Log array split
For example, insert a piece of data
Db.test1.insert ({_ id:1,size: [111222333]})
# split
Db.test1.aggregate (
{$unwind:'$size'}
)
Will output:
{"_ id": 1, "size": 111}
{"_ id": 1, "size": 222}
{"_ id": 1, "size": 333}
Indexes
# insert 1000 pieces of data to execute the js script in MongoDB
# you can insert more data to see better results
For (iSuppli)