May 20, 2021 WeChat Mini Program Development Document
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.
Positive integer
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
}