如何获取文档的数量并以有效的方式过滤它们? (猫鼬)

2023-12-12

我正在实现搜索功能,即在 mongoDB 中查找文档。我想要.skip(x) and .limit(x)on result 来模拟分页结果,但是我可以获取文档总数(在跳过和限制之前)并立即获取过滤结果吗?

产生预期输出的代码:

db.Datas.find({ type: "Unknown" })
  .then((result) => {
    let count = result.length;
    db.Datas.find({ type: "Unknown" })
      .sort({ createdAt: -1 })
      .skip((req.query.page - 1) * 10)
      .limit(10)
      .then((res) => {
        res.json({ count: count, result: res });
      });
  })
  .catch((err) => {});

但查询两次有点烦人,而且在大型数据库中可能会很慢。 我尝试过类似的东西find({}).then(x => { ... }).sort(...) ...但不起作用,因为它只返回 Promise。

我怎样才能有效地做这些事情? 或者只是获取整个文档并跳过,用 JS 方式限制(使用 .splice 等)会更快更高效?


您可以使用$facet聚合来实现这一点。

db.Datas.aggregate([
  {
    $match: {
      "type": "Unknown"
    }
  },
  {
    $sort: {
      createdAt: -1
    }
  },
  {
    $facet: {
      totalRecords: [
        {
          $count: "total"
        }
      ],
      data: [
        {
          $skip: 0
        },
        {
          $limit: 5
        }
      ]
    }
  }
])

操场

假设您有这些文件:

db={
  "Datas": [
    {
      "_id": "5e390fc33285e463a0799689",
      "type": "Known",
      "createdAt": "2020-02-04T06:31:31.311Z",
      "__v": 0
    },
    {
      "_id": "5e390fd03285e463a079968a",
      "type": "Known",
      "createdAt": "2020-02-04T06:31:44.190Z",
      "__v": 0
    },
    {
      "_id": "5e390fda3285e463a079968b",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:31:54.248Z",
      "__v": 0
    },
    {
      "_id": "5e390fdf3285e463a079968c",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:31:59.993Z",
      "__v": 0
    },
    {
      "_id": "5e390fec3285e463a079968d",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:32:12.336Z",
      "__v": 0
    },
    {
      "_id": "5e390ffd3285e463a079968e",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:32:29.670Z",
      "__v": 0
    },
    {
      "_id": "5e3910163285e463a079968f",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:32:54.131Z",
      "__v": 0
    },
    {
      "_id": "5e3910213285e463a0799690",
      "type": "Unknown",
      "createdAt": "2020-02-04T06:33:05.166Z",
      "__v": 0
    }
  ]
}

响应将是这样的:

[
  {
    "data": [
      {
        "__v": 0,
        "_id": "5e3910213285e463a0799690",
        "createdAt": "2020-02-04T06:33:05.166Z",
        "type": "Unknown"
      },
      {
        "__v": 0,
        "_id": "5e3910163285e463a079968f",
        "createdAt": "2020-02-04T06:32:54.131Z",
        "type": "Unknown"
      },
      {
        "__v": 0,
        "_id": "5e390ffd3285e463a079968e",
        "createdAt": "2020-02-04T06:32:29.670Z",
        "type": "Unknown"
      },
      {
        "__v": 0,
        "_id": "5e390fec3285e463a079968d",
        "createdAt": "2020-02-04T06:32:12.336Z",
        "type": "Unknown"
      },
      {
        "__v": 0,
        "_id": "5e390fdf3285e463a079968c",
        "createdAt": "2020-02-04T06:31:59.993Z",
        "type": "Unknown"
      }
    ],
    "totalRecords": [
      {
        "total": 6
      }
    ]
  }
]

正如你所看到的,我们得到了经过过滤、排序、跳过和有限数据的总记录。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取文档的数量并以有效的方式过滤它们? (猫鼬) 的相关文章

随机推荐