• <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>

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

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

          <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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (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>
                              <tbody id='df2cP'></tbody>
                          • <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>