The previous blog post describes how the collation collations is manipulated and set up. Incidentally, some of the aggregation aggregation are set up in this way. This article continues to describe aggregation operations.
The operation of the aggregation framework finishes processing data records after it returns the calculated results. A collection operation classifies values from multiple documents together, so that there is a suspicion of multiple operations on the data being classified, and then returns a separate result
1 converged Piping
Aggregation pipelines are a framework for data aggregation and are based on the concept of data processing pipelines. After you enter a document into a multistage pipeline, the suspect converts the document to an aggregated result. With restaurants as the dataset, we can use the aggregation pipeline to find the total number of 5-star restaurants in the collection by categorizing the restaurant class.
client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') coll= Client[:restaurants]aggregation=coll.aggregate ([ {' $match ' = {' Stars ' =>5}}, {' $unwind ' = ' $categories '}, {' $group ' =>{' _id ' = ' $categories ', ' fivestars ' =>{' $num ' =>1}}} ]) aggregation.each do |doc| p doc end
In the above program, within the aggregate method, the first parameter filters out the document with the Stars field of 5 from all documents. The second parameter, unwind, indicates that the field of the Categories field is expanded, which is an array of entries in the array as separate documents. The third parameter classifies the document according to categories and then counts the number of five-star hotels.
The aggregation framework has a maximum usage memory limit. So in order to handle large data sets, you need to set the Allowdiskuse parameter to true to ensure that the data can be written to a permanent file.
Aggregation=coll.aggregate ([<aggregation Pipeline expressions]) Aggregation_with_disk_use=aggregation.allow_ Disk_use (True)
Or you can pass parameters to the aggregate method.
Aggregation=coll.aggregate ([<aggregation pipeline Expressions>],:allow_disk_use=>true)
2. Simple Purpose Aggregation operation
MongoDB provides support for some aggregate functions, including count and distinct
2.1 Count
The following example shows us how to find the total number of documents in the collection that categories domain contains data [' Chinese ', ' Seafood '] collection.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') coll=client[:restaurants]aggregation = Coll.count ({' Categories ': [' Chinese ', ' Seafood ']}) Count=coll.count ({' Categories ' =>[' Chinese ', ' Seafood ']})
2.2 Distinct
The distinct method is used to remove duplicate data from the resulting dataset, returning a separate value for each record. The following example finds all the non-repeating data for the Categories field fields on the collection restaurants.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') coll=client[:restaurants]aggregation= Coll.distinct (' categories ') Aggregation.each do |doc| P Doc End
This concludes the tutorial on aggregating operations in MongoDB
This article from "Techfuture" blog, declined reprint!
Ruby Operation MongoDB (step eight)-aggregation operation aggregation