• <tfoot id='G292E'></tfoot>

      • <bdo id='G292E'></bdo><ul id='G292E'></ul>

      <small id='G292E'></small><noframes id='G292E'>

      1. <i id='G292E'><tr id='G292E'><dt id='G292E'><q id='G292E'><span id='G292E'><b id='G292E'><form id='G292E'><ins id='G292E'></ins><ul id='G292E'></ul><sub id='G292E'></sub></form><legend id='G292E'></legend><bdo id='G292E'><pre id='G292E'><center id='G292E'></center></pre></bdo></b><th id='G292E'></th></span></q></dt></tr></i><div id='G292E'><tfoot id='G292E'></tfoot><dl id='G292E'><fieldset id='G292E'></fieldset></dl></div>
      2. <legend id='G292E'><style id='G292E'><dir id='G292E'><q id='G292E'></q></dir></style></legend>

        我怎样才能获得文档的数量并以有效的方式过滤它们?(猫鼬)

        时间:2023-10-02

          <legend id='37EFD'><style id='37EFD'><dir id='37EFD'><q id='37EFD'></q></dir></style></legend>

            <small id='37EFD'></small><noframes id='37EFD'>

            • <bdo id='37EFD'></bdo><ul id='37EFD'></ul>

                <i id='37EFD'><tr id='37EFD'><dt id='37EFD'><q id='37EFD'><span id='37EFD'><b id='37EFD'><form id='37EFD'><ins id='37EFD'></ins><ul id='37EFD'></ul><sub id='37EFD'></sub></form><legend id='37EFD'></legend><bdo id='37EFD'><pre id='37EFD'><center id='37EFD'></center></pre></bdo></b><th id='37EFD'></th></span></q></dt></tr></i><div id='37EFD'><tfoot id='37EFD'></tfoot><dl id='37EFD'><fieldset id='37EFD'></fieldset></dl></div>
                    <tbody id='37EFD'></tbody>
                  <tfoot id='37EFD'></tfoot>

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

                  问题描述

                  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.

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

                  上一篇:MongoDB - 使用聚合管道评论赞成/反对 下一篇:如何使用 window.fetch 下载文件?

                  相关文章

                • <tfoot id='df2cP'></tfoot>

                • <legend id='df2cP'><style id='df2cP'><dir id='df2cP'><q id='df2cP'></q></dir></style></legend>

                  1. <small id='df2cP'></small><noframes id='df2cP'>

                        <bdo id='df2cP'></bdo><ul id='df2cP'></ul>
                    1. <i id='df2cP'><tr id='df2cP'><dt id='df2cP'><q id='df2cP'><span id='df2cP'><b id='df2cP'><form id='df2cP'><ins id='df2cP'></ins><ul id='df2cP'></ul><sub id='df2cP'></sub></form><legend id='df2cP'></legend><bdo id='df2cP'><pre id='df2cP'><center id='df2cP'></center></pre></bdo></b><th id='df2cP'></th></span></q></dt></tr></i><div id='df2cP'><tfoot id='df2cP'></tfoot><dl id='df2cP'><fieldset id='df2cP'></fieldset></dl></div>