我怎样才能获得文档的数量并以有效的方式过滤它们?(猫鼬) [英] How can I get count of the Documents and filter them in efficient way? (mongoose)

查看:13
本文介绍了我怎样才能获得文档的数量并以有效的方式过滤它们?(猫鼬)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm implementing search function that is simply find document in mongoDB. I want to .skip(x) and .limit(x) on result to simulate paging result, but can I get total count of document (before skip and limit) and get filtered result at once?

Code that produce Expected Output :

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) => {});

But querying twice it somehow annoying, and it might be slow at large database. I tried something like find({}).then(x => { ... }).sort(...) ... but isn't working because it only returns Promise.

How can I do this things in efficient way? or is just getting whole documents and skip, limit with JS-way (using .splice, or etc..) will be faster and efficient?

解决方案

You can use $facet aggregation to achieve this.

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

Playground

Let's say you have these documents:

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
    }
  ]
}

Response will be like this:

[
  {
    "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
      }
    ]
  }
]

As you see, we got the total records with the filtered, sorted, skipped and limited data.

这篇关于我怎样才能获得文档的数量并以有效的方式过滤它们?(猫鼬)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆