The concept of MongoDB pipelines

an introduction
Pipes are commonly used in Unix and Linux to pass the output of the current command as an argument to the next command.
MongoDB's aggregation pipeline takes MongoDB documents after processing one pipeline and passes the result to the next pipeline for processing. Pipeline operations are repeatable.
Expression: Process the input document and output it. Expressions are stateless and can only be used to evaluate documents in the current aggregation pipeline, not other documents.
Here we introduce several operations commonly used in the aggregation framework:
$project: Modify the structure of the input document. Can be used to rename, add or delete fields, and can also be used to create calculated results and nested documents.
$match: Used to filter data and only output documents that meet the conditions. $match uses MongoDB's standard query operations.
$limit: Used to limit the number of documents returned by the MongoDB aggregation pipeline.
$skip: Skip the specified number of documents in the aggregation pipeline and return the remaining documents.
$unwind: Splits an array type field in the document into multiple entries, each containing a value in the array.
$group: Groups the documents in the collection, which can be used for statistical results.
$sort: Sort the input documents and output them.
$geoNear: Output ordered documents close to a geographic location.
 
Two pipe operator examples
1. $project instance
  1. db.article.aggregate(
  2. { $project : {
  3. title : 1 ,
  4. author : 1 ,
  5. }}
  6. );
In this case, there are only three fields _id, tilte and author in the result. By default, the _id field is included. If you want to exclude _id, you can do this:
  1. db.article.aggregate(
  2. { $project : {
  3. _id : 0 ,
  4. title : 1 ,
  5. author : 1
  6. }});
2. $match instance
  1. db.articles.aggregate( [
  2. { $match : { score : { $gt : 70, $lte : 90 } } },
  3. { $group: { _id: null, count: { $sum: 1 } } }
  4. ] );
$match is used to obtain records with scores greater than 70 and less than or equal to 90, and then send eligible records to the next stage $group pipeline operator for processing.
3. $skip instance
  1. db.article.aggregate(
  2. { $skip : 5 });
After being processed by the $skip pipe operator, the first five documents are "filtered" out.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040667&siteId=291194637