滕磊的个人网站
javascript css html mongodb typescript linux docker mysql 美文 经典语录 励志 句子摘抄 名人名言 座右铭
» 栏目 » mongoose教程 » mongoose随机取n条数据

mongoose随机取n条数据

栏目: mongoose教程 发布时间:2021-12-24

如何使用mongoose随机取n条数据?我们可以使用aggregate方法实现随机取n条数据。

db.tags.aggregate(
  [{
    $sample: {
      size: N
    }
  }]
)
'use strict';

const Service = require('egg').Service;

class TagService extends Service {
  async list() {
    return await this.ctx.model.Tag.aggregate(
      [{
        $sample: {
          size: 50,
        },
      }]
    );
  }
}

module.exports = TagService;

本文地址:https://www.tides.cn/p_mongoose-aggregate

标签: aggregate $sample

推荐阅读

Mongoose使用教程 mongoose or查询方法 mongoose查询文档教程 (node:43032) DeprecationWarning: Mongoose: findOneAndUpdate() and findOneAndDelete() without the useFindAndModify option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify (Use node --trace-deprecation ... to show where the warning was created) mongoose and查询方法 mongoose删除列的方法 Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit. mongoose排序查询sort方法 $sample stage could not find a non-duplicate document after 100 while using a random cursor 如何使用 mongoose 对数组中数组中的元素进行查询? 微信小程序在后台运行多久会被回收? Chrome浏览器插件从Manifest V2迁移到Manifest V3迁移指南

简体字繁体字转换工具 图片转base64编码 rgb转16进制 rgb颜色对照表 在线小工具 古诗词 古诗词问答

Copyright @tides.cn 苏ICP备20031417号-2