Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SDK database Aggregate limits the number of records


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.limit(value: number): Aggregate

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregation phase. Limit the number of records output to the next stage.

Parameters

value: number

Positive integer

Returns a value

Aggregate

Example

Suppose the collection items have the following records:

{
  _id: "1",
  price: 10
}
{
  _id: "2",
  price: 50
}
{
  _id: "3",
  price: 20
}
{
  _id: "4",
  price: 80
}
{
  _id: "5",
  price: 200
}

Returns the smallest two records for records with prices greater than 20:

const $ = db.command.aggregate
db.collection('items').aggregate()
  .match({
    price: $.gt(20)
  })
  .sort({
    price: 1,
  })
  .limit(2)
  .end()

The results are as follows:

{
  "_id": "3",
  "price": 20
}
{
  "_id": "4",
  "price": 80
}