MongoDB database data aggregation & amp; Pipe operations, mongodb database Aggregation
The data aggregation process for pipeline operations on mongodb usually works with pipeline operations. The pipeline operation concept of mongodb is similar to the pipeline concept in LInux, the mongodb aggregation pipeline transmits the results to the next pipeline after processing the mongodb documents in one pipeline. pipeline operations can be repeated in this way;
The mongodb MPs queue uses the aggregte () method and uses the MPs queue expression to represent a MPs queue process. The expressions are stateless and can only calculate documents of the current aggregated MPs queue, but cannot process other documents, the following are common pipeline operation expressions:
Aggregate operations use the aggregate () function. The prototype of this function is:Db. collection_name.aggregate ( )
Common expressions of the opeation parameter:
Expression |
Description |
$ Project |
Modify the structure of the input document. It can be used to rename, add/delete domains, create computing results, and nest documents; |
$ Match |
Used to filter data and only output qualified documents. $ match uses the MongoDB standard query operation; |
$ Limit |
Used to limit the number of documents returned by the MongoDB aggregation pipeline; |
$ Skip |
Skip a specified number of documents in the aggregation pipeline and return the remaining documents; |
$ Count |
Displays the number of documents in the current MPs queue. |
$ Size |
Displays the length of an array for a key (which is an array type; |
$ Unwind |
Split an array field in the document into multiple entries, each containing a value in the array; |
$ Group |
Groups Documents in the set for statistical results; |
$ Sort |
Sort the input documents and output them; |
$ GeoNear |
Output ordered documents close to a certain geographical location; |
For how to use the pipeline complete expression list, see: https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/
Example: Use the document structure of the collection blog as follows:
?
{
? _id: ObjectId("5a88f21ac05302a3b998abb9"),
? title: 'MongoDB Overview',
? description: 'MongoDB is no sql database',
? author: 'assad',
? url: 'https://blog.assad.artcile/1',
? tags: ['mongodb', 'database', 'NoSQL'],
? likes: 100
},
{
? _id: ObjectId("5a88f278c05302a3b998abba"),
? title: 'NoSQL Overview',
? description: 'No sql database is very fast',
? author: 'Tim',
? url: 'https://blog.assad.artcile/34',
? tags: ['mongodb', 'database', 'NoSQL'],
? likes: 233
},
{
? _id: ObjectId("5a88f299c05302a3b998abbb"),
? title: 'SqlLite Overview',
? description: 'SqlLite is no sql database',
? author: 'assad',
? url: 'https://blog.assad.artcile/213',
? tags: ['SqlLite', 'database', 'NoSQL'],
? likes: 666
}
Example :?
# $ Project example: only the title, author, and likes fields of the blog set are displayed.
> db.blog.aggregate(
... ? ? [ {$project:{_id:0,title:1,author:1,likes:1}} ]
... )
?
{ "author" : "assad", "likes" : 100, "title" : "article_title" }
{ "author" : "Tim", "likes" : 233, "title" : "article_title" }
{ "author" : "assad", "likes" : 666, "title" : "article_title" }
?
# $ Count example: displays the number of likes> 100 and likes <500 in the blog collection.
> db.blog.aggregate([
... { $match:{likes:{$gt:100,$lt:500}} },
... { $count:"result" }
... ])
{ "result" : 1 }
?
# Example: displays the total number of tages for each document in the blog set.
> db.blog.aggregate([
... ? { $project:{_id:0,title:1,tages_count:{$size:"$tags"}} }
...])
?
{ "title" : "MongoDB Overview", "tages_count" : 3 }
{ "title" : "NoSQL Overview", "tages_count" : 3 }
{ "title" : "SqlLite Overview", "tages_count" : 3 }
?
# Comprehensive example: show the author = "assad", likes> = 100, likes <= 800 documents in the blog collection, sorted by likes in the forward order, only the first five documents are displayed, only the title, author, and likes fields are displayed.
> db.blog.aggregate( [
... { $project:{title:1,author:1,likes:1} },
... { $match:{author:"assad",likes:{$gte:100,$lte:800}} },
... { $limit:5 },
... { $sort:{likes:1} }
... ]}
?
{ "_id" : ObjectId("5a88f21ac05302a3b998abb9"), "title" : "MongoDB Overview", "author" : "assad", "likes" : 100 }
{ "_id" : ObjectId("5a88f299c05302a3b998abbb"), "title" : "SqlLite Overview", "author" : "assad", "likes" : 666 }
?
# Comprehensive example: displays the statistics of the number of occurrences of each value in the tags key array in the blog collection.
> db.blog.aggregate([
... ? { $unwind:"$tags" },
... ? { $group:{_id:"$tags",count:{$sum:1}}}
... ])
?
{ "_id" : "SqlLite", "count" : 1 }
{ "_id" : "NoSQL", "count" : 3 }
{ "_id" : "database", "count" : 3 }
{ "_id" : "mongodb", "count" : 2 }
$ Group expressionIn the aggregate () method, use the $ group expression for grouping operations. Similar to the group by clause in SQL, the common aggregate expressions of $ group are as follows:
Expression |
Description |
$ Sum |
Calculates the sum; |
$ Avg |
Calculate the average value; |
$ Max |
Obtains the maximum key value of a document; |
$ Min |
Obtains the minimum value of the key value corresponding to the document; |
$ First |
Obtain the first document according to the document order; |
$ Last |
Obtain the last document according to the document order; |
$ Push |
Insert an array into the result document; |
$ AddToSet |
Insert an array in the result document without creating a copy; |
Example: $ sum?
# Display the total likes of each author
> db.blog.aggregate(
... ? [ {$group:{_id:"$author",likes_count:{$sum:"$likes"} }} ]
...)
?
{ "_id" : "Tim", "likes_count" : 233 }
{ "_id" : "assad", "likes_count" : 766 }
?
# Display the likes of all documents
> db.blog.aggregate(
... ? [ {$group:{_id:null, likes_count:{$sum:"$likes"} }} ]
...)
?
{ "_id" : null, "likes_count" : 999 }
Demo $ avg
?
# Display the likes average of each author
> db.blog.aggregate(
... ? [ {$group:{_id:"$author",likes_avg:{$avg:"$likes"} }} ]
...)
?
{ "_id" : "Tim", "likes_avg" : 233 }
{ "_id" : "assad", "likes_avg" : 383 }
?
# Display the likes average of all documents
> db.blog.aggregate(
... ? [ {$group:{_id:null, likes_avg:{$avg:"$likes"} }} ]
...)
?
{ "_id" : null, "likes_avg" : 333 }
?
Demo $ min, $ max
?
# Display likes minimum values for each author
> db.blog.aggregate(
... ? ? [ {$group:{_id:"$author",likes_min:{$min:"$likes"} }} ]
... )
?
{ "_id" : "Tim", "likes_min" : 233 }
{ "_id" : "assad", "likes_min" : 100 }
?
# Display the minimum likes record in all document records
> db.blog.aggregate(
... ? ? [ {$group:{_id:null, likes_min:{$min:"$likes"} }} ]
... )
?
{ "_id" : null, "likes_min" : 100 }
?
# Display the maximum likes records of all document records
> db.blog.aggregate(
... ? ? [ {$group:{_id:null, likes_max:{$max:"$likes"} }} ]
... )
?
{ "_id" : null, "likes_max" : 666 }
Demo? $ Push
?
# Display the article title and likes lists of author
> db.blog.aggregate(
... ? ? [ {$group:{_id:"$author",article_like:{$push:{title:"$title",likes:"$likes"}}}} ]
... )
?
{ "_id" : "Tim", "article_like" : [ { "title" : "NoSQL Overview", "likes" : 233 } ] }
{ "_id" : "assad", "article_like" : [ { "title" : "MongoDB Overview", "likes" : 100 }, { "article" : "SqlLite Overview", "likes" : 666 } ] }
Group and aggregate by TimeFor Grouping and aggregation of time by year, month, day, hour, minute, and second, you can use the following expression to obtain the time parameter: $ dayOfYear: returns the day (366 days of the year) of the year ).
$ DayOfMonth: returns the Day (1 to 31) of the month ).
$ DayOfWeek: returns the day of the week (1: Sunday, 7: Saturday ).
$ Year: returns the year of the date.
$ Month: returns the month of the date (1 to 12 ).
$ Week: returns the week (0 to 53) of the year to which the date belongs ).
$ Hour: returns the hour of the date.
$ Minute: returns the minute of the date.
$ Second: returns the second part of the date (returns the second part of the date in the form of a number between 0 and 59, but can be 60 to calculate the leap second ).
$ Millisecond: returns the millisecond portion of the date (0 to 999 ).
$ DateToString :{$ dateToString: {format:, date :}}.
The following is the sample loginLog set content:
?
{
? ? ? ?"_id" : ObjectId("5a8969f54a7c036151cce245"),
? ? ? ?"ip" : "25.12.4.42",
? ? ? ?"date" : ISODate("2018-02-16T09:33:12Z")
}
{
? ? ? ?"_id" : ObjectId("5a896a164a7c036151cce246"),
? ? ? ?"ip" : "126:123:11:1",
? ? ? ?"date" : ISODate("2018-02-17T10:20:00Z")
}
{
? ? ? ?"_id" : ObjectId("5a896a2c4a7c036151cce247"),
? ? ? ?"ip" : "23.88.44.22",
? ? ? ?"date" : ISODate("2018-01-20T20:30:00Z")
}
{
? ? ? ?"_id" : ObjectId("5a896a4a4a7c036151cce248"),
? ? ? ?"ip" : "55.78.22.123",
? ? ? ?"date" : ISODate("2018-02-19T21:59:20Z")
}
>
Example:
?
# Count the total number of documents grouped by month
> db.loginLog.aggregate([
... ? ? { $group:{_id:{$month:"$date"},log_count:{$sum:1}} }
... ])
?
{ "_id" : 1, "log_count" : 1 }
{ "_id" : 2, "log_count" : 3 }