This is the 28th day of my participation in Gwen Challenge

preface

This paper introduces the array matching filter, field filter, grouping, summation, maximum and minimum value content. This article continues with the useful array aggregation operations, using the same test data as in the previous article.

Array expansion $unwind

Array expansion is for a field whose data type is array and can be expanded into a single document data for each element of the array so that other operations can be performed.

db.employees.aggregate(
	{$match: {name: 'Island code farmer'}}, 
  {$unwind: '$languages'}
).pretty();
Copy the code

The result is:

{
	"_id" : ObjectId("60d734f0d8079507891982a8"),
	"name" : "Island code farmer"."dept" : "R&d Department"."languages" : "Dart"."age" : 30."totalExp" : 10
}
{
	"_id" : ObjectId("60d734f0d8079507891982a8"),
	"name" : "Island code farmer"."dept" : "R&d Department"."languages" : "Java"."age" : 30."totalExp" : 10
}
{
	"_id" : ObjectId("60d734f0d8079507891982a8"),
	"name" : "Island code farmer"."dept" : "R&d Department"."languages" : "Javascript"."age" : 30."totalExp" : 10
}

Copy the code

The sorting

You can specify sorting by a field in the aggregation operation, where 1 corresponds to ascending order and -1 to descending order.

db.employees.aggregate(
	[
  	{$match: {dept: '研发部'}}, 
  	{$project: {'name': 1, 'dept': 1, 'age': 1}}, 
  	{$sort: {'age': 1}}
  ]
).pretty();
Copy the code
{
	"_id" : ObjectId("60d734f0d8079507891982ad"),
	"name" : "Jenny"."dept" : "R&d Department"."age" : 26
}
{
	"_id" : ObjectId("60d734f0d8079507891982a8"),
	"name" : "Island code farmer"."dept" : "R&d Department"."age" : 30
}
{
	"_id" : ObjectId("60d734f0d8079507891982ab"),
	"name" : "Cathy"."dept" : "R&d Department"."age" : 31
}
{
	"_id" : ObjectId("60d734f0d8079507891982a9"),
	"name" : "Amy"."dept" : "R&d Department"."age" : 35
}
Copy the code

Skip a specified number of documents

Use the $sort operator to specify the number of preceding elements to skip. For example, the above example filters out the first three documents, leaving only the fourth element Amy.

db.employees.aggregate(
	[
  	{$match: {dept: '研发部'}}, 
  	{$project: {'name': 1, 'dept': 1, 'age': 1}}, 
  	{$sort: {'age': 1}},
    {$skip: 3}
  ]
).pretty();
Copy the code

Limit the number of documents returned

Use the $limit operator to limit the number of documents returned.

db.employees.aggregate(
	[
  	{$match: {dept: '研发部'}}, 
  	{$project: {'name': 1, 'dept': 1, 'age': 1}}, 
  	{$sort: {'age': 1}},
    {$limit: 2}
  ]
).pretty();
Copy the code

$skip can also be combined with $skip, but note that the order of $skip and $limit is different. $skip will skip the first two documents and then restrict, while $limit will return a limited number of documents and then skip the previous documents. With this combination you can actually achieve sqL-like paging effects.

db.employees.aggregate(
	[
  	{$match: {dept: '研发部'}}, 
  	{$project: {'name': 1, 'dept': 1, 'age': 1}}, 
  	{$sort: {'age': 1}},
    {$skip: 2},
    {$limit:2},
  ]
).pretty();
Copy the code

conclusion

$skip, $limit, $skip, $limit, $unwind, $unwind, $unwind, $sort, $skip, $limit, etc.