I heard that blog garden collapse recently, can only be published to the nuggets…..

The focus of this paper is: Mongoose’s aggregate aggregate pipeline, without talking about the principle, how to obtain random data directly

The official address: www.mongoosejs.net/docs/api.ht…

There are three ways to write it:

Activity.aggregate([
    {
        $match: {
            'act_status': '1'.'verifycode': 1
        }
    }
 ]).sample(6);
Copy the code

The second way is:

Activity.aggregate([
    {
        $match: {
            'act_status': '1'.'verifycode': 1
        }
    },{
        $sample: {
            size: 6}}]);Copy the code

The third (and more concise) way:

Activity.aggregate().match({
    'act_status': '1'.'verifycode': 1
}).sample(6);
Copy the code

$match is the filter condition, and $sample is the number of random data items to be obtained