Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从MongoDB集合以10块的形式加载文档的最简单方法_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 从MongoDB集合以10块的形式加载文档的最简单方法

Node.js 从MongoDB集合以10块的形式加载文档的最简单方法,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想创建一个应用程序,创建订单,然后将其加载到列表中。问题是我不能一次加载所有订单。我想把它们10个10个装进去 然而,这在MongoDB中看起来非常困难,因为在SQL中没有自动id。我知道如何模拟自动id,但我认为实现此功能应该更容易 因此,对于MongoDB集合,如何以10×10的方式加载文档,从最新的文档到最新的文档?您需要按字段对文档进行排序,然后使用聚合(&a)。此外,为了获得记录的总数,我们可以使用聚合 以下是详细说明: 假设您在订单集合中有这8个文档 [ {

我想创建一个应用程序,创建订单,然后将其加载到列表中。问题是我不能一次加载所有订单。我想把它们10个10个装进去

然而,这在MongoDB中看起来非常困难,因为在SQL中没有自动id。我知道如何模拟自动id,但我认为实现此功能应该更容易


因此,对于MongoDB集合,如何以10×10的方式加载文档,从最新的文档到最新的文档?

您需要按字段对文档进行排序,然后使用聚合(&a)。此外,为了获得记录的总数,我们可以使用聚合

以下是详细说明:

假设您在订单集合中有这8个文档

[
    {
        "_id": "5e390fc33285e463a0799689",
        "customer": "Max",
        "total": 10,
        "orderDate": "2020-02-04T06:31:31.311Z",
        "__v": 0
    },
    {
        "_id": "5e390fd03285e463a079968a",
        "customer": "John",
        "total": 9.2,
        "orderDate": "2020-02-04T06:31:44.190Z",
        "__v": 0
    },
    {
        "_id": "5e390fda3285e463a079968b",
        "customer": "Smith",
        "total": 11.3,
        "orderDate": "2020-02-04T06:31:54.248Z",
        "__v": 0
    },
    {
        "_id": "5e390fdf3285e463a079968c",
        "customer": "Smith",
        "total": 12.3,
        "orderDate": "2020-02-04T06:31:59.993Z",
        "__v": 0
    },
    {
        "_id": "5e390fec3285e463a079968d",
        "customer": "Jimmy",
        "total": 15.6,
        "orderDate": "2020-02-04T06:32:12.336Z",
        "__v": 0
    },
    {
        "_id": "5e390ffd3285e463a079968e",
        "customer": "Wesley",
        "total": 11,
        "orderDate": "2020-02-04T06:32:29.670Z",
        "__v": 0
    },
    {
        "_id": "5e3910163285e463a079968f",
        "customer": "Adam",
        "total": 6.1,
        "orderDate": "2020-02-04T06:32:54.131Z",
        "__v": 0
    },
    {
        "_id": "5e3910213285e463a0799690",
        "customer": "Michael",
        "total": 7.2,
        "orderDate": "2020-02-04T06:33:05.166Z",
        "__v": 0
    }
]
如果我们想将这些文档分块,我们可以编写一个示例路由,如下所示:

router.get(“/orders”),异步(req,res)=>{
const page=req.query.pageIndex?+req.query.pageIndex:1;
const limit=req.query.pageSize?+req.query.pageSize:10;
常量跳过=(第1页)*限制;
const result=wait Order.aggregate([
{
$sort:{
订单日期:-1
}
},
{
$facet:{
totalRecords:[{$count:“total”}],
数据:[{$skip:skip},{$limit:limit}]
}
}
]);
res.send(结果);
});
我们在查询字符串中发送pageIndex和pageSize参数,如下
http://...../orders?pageIndex=1&pageSize=3

当我们使用
pageIndex=1
pageSize=3
时,结果如下:(如您所见,我们还返回记录总数,以便客户端可以生成分页编号)

当我们使用
pageIndex=2
pageSize=3
时,结果如下:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fec3285e463a079968d",
                "customer": "Jimmy",
                "total": 15.6,
                "orderDate": "2020-02-04T06:32:12.336Z",
                "__v": 0
            },
            {
                "_id": "5e390fdf3285e463a079968c",
                "customer": "Smith",
                "total": 12.3,
                "orderDate": "2020-02-04T06:31:59.993Z",
                "__v": 0
            },
            {
                "_id": "5e390fda3285e463a079968b",
                "customer": "Smith",
                "total": 11.3,
                "orderDate": "2020-02-04T06:31:54.248Z",
                "__v": 0
            }
        ]
    }
]
[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fd03285e463a079968a",
                "customer": "John",
                "total": 9.2,
                "orderDate": "2020-02-04T06:31:44.190Z",
                "__v": 0
            },
            {
                "_id": "5e390fc33285e463a0799689",
                "customer": "Max",
                "total": 10,
                "orderDate": "2020-02-04T06:31:31.311Z",
                "__v": 0
            }
        ]
    }
]
当我们使用
pageIndex=3
pageSize=3
时,结果如下:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fec3285e463a079968d",
                "customer": "Jimmy",
                "total": 15.6,
                "orderDate": "2020-02-04T06:32:12.336Z",
                "__v": 0
            },
            {
                "_id": "5e390fdf3285e463a079968c",
                "customer": "Smith",
                "total": 12.3,
                "orderDate": "2020-02-04T06:31:59.993Z",
                "__v": 0
            },
            {
                "_id": "5e390fda3285e463a079968b",
                "customer": "Smith",
                "total": 11.3,
                "orderDate": "2020-02-04T06:31:54.248Z",
                "__v": 0
            }
        ]
    }
]
[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fd03285e463a079968a",
                "customer": "John",
                "total": 9.2,
                "orderDate": "2020-02-04T06:31:44.190Z",
                "__v": 0
            },
            {
                "_id": "5e390fc33285e463a0799689",
                "customer": "Max",
                "total": 10,
                "orderDate": "2020-02-04T06:31:31.311Z",
                "__v": 0
            }
        ]
    }
]

对于您的情况,您需要发送10作为pageSize值。

您需要按字段对文档进行排序,然后使用聚合(&a)。此外,为了获得记录的总数,我们可以使用聚合

以下是详细说明:

假设您在订单集合中有这8个文档

[
    {
        "_id": "5e390fc33285e463a0799689",
        "customer": "Max",
        "total": 10,
        "orderDate": "2020-02-04T06:31:31.311Z",
        "__v": 0
    },
    {
        "_id": "5e390fd03285e463a079968a",
        "customer": "John",
        "total": 9.2,
        "orderDate": "2020-02-04T06:31:44.190Z",
        "__v": 0
    },
    {
        "_id": "5e390fda3285e463a079968b",
        "customer": "Smith",
        "total": 11.3,
        "orderDate": "2020-02-04T06:31:54.248Z",
        "__v": 0
    },
    {
        "_id": "5e390fdf3285e463a079968c",
        "customer": "Smith",
        "total": 12.3,
        "orderDate": "2020-02-04T06:31:59.993Z",
        "__v": 0
    },
    {
        "_id": "5e390fec3285e463a079968d",
        "customer": "Jimmy",
        "total": 15.6,
        "orderDate": "2020-02-04T06:32:12.336Z",
        "__v": 0
    },
    {
        "_id": "5e390ffd3285e463a079968e",
        "customer": "Wesley",
        "total": 11,
        "orderDate": "2020-02-04T06:32:29.670Z",
        "__v": 0
    },
    {
        "_id": "5e3910163285e463a079968f",
        "customer": "Adam",
        "total": 6.1,
        "orderDate": "2020-02-04T06:32:54.131Z",
        "__v": 0
    },
    {
        "_id": "5e3910213285e463a0799690",
        "customer": "Michael",
        "total": 7.2,
        "orderDate": "2020-02-04T06:33:05.166Z",
        "__v": 0
    }
]
如果我们想将这些文档分块,我们可以编写一个示例路由,如下所示:

router.get(“/orders”),异步(req,res)=>{
const page=req.query.pageIndex?+req.query.pageIndex:1;
const limit=req.query.pageSize?+req.query.pageSize:10;
常量跳过=(第1页)*限制;
const result=wait Order.aggregate([
{
$sort:{
订单日期:-1
}
},
{
$facet:{
totalRecords:[{$count:“total”}],
数据:[{$skip:skip},{$limit:limit}]
}
}
]);
res.send(结果);
});
我们在查询字符串中发送pageIndex和pageSize参数,如下
http://...../orders?pageIndex=1&pageSize=3

当我们使用
pageIndex=1
pageSize=3
时,结果如下:(如您所见,我们还返回记录总数,以便客户端可以生成分页编号)

当我们使用
pageIndex=2
pageSize=3
时,结果如下:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fec3285e463a079968d",
                "customer": "Jimmy",
                "total": 15.6,
                "orderDate": "2020-02-04T06:32:12.336Z",
                "__v": 0
            },
            {
                "_id": "5e390fdf3285e463a079968c",
                "customer": "Smith",
                "total": 12.3,
                "orderDate": "2020-02-04T06:31:59.993Z",
                "__v": 0
            },
            {
                "_id": "5e390fda3285e463a079968b",
                "customer": "Smith",
                "total": 11.3,
                "orderDate": "2020-02-04T06:31:54.248Z",
                "__v": 0
            }
        ]
    }
]
[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fd03285e463a079968a",
                "customer": "John",
                "total": 9.2,
                "orderDate": "2020-02-04T06:31:44.190Z",
                "__v": 0
            },
            {
                "_id": "5e390fc33285e463a0799689",
                "customer": "Max",
                "total": 10,
                "orderDate": "2020-02-04T06:31:31.311Z",
                "__v": 0
            }
        ]
    }
]
当我们使用
pageIndex=3
pageSize=3
时,结果如下:

[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fec3285e463a079968d",
                "customer": "Jimmy",
                "total": 15.6,
                "orderDate": "2020-02-04T06:32:12.336Z",
                "__v": 0
            },
            {
                "_id": "5e390fdf3285e463a079968c",
                "customer": "Smith",
                "total": 12.3,
                "orderDate": "2020-02-04T06:31:59.993Z",
                "__v": 0
            },
            {
                "_id": "5e390fda3285e463a079968b",
                "customer": "Smith",
                "total": 11.3,
                "orderDate": "2020-02-04T06:31:54.248Z",
                "__v": 0
            }
        ]
    }
]
[
    {
        "totalRecords": [
            {
                "total": 8
            }
        ],
        "data": [
            {
                "_id": "5e390fd03285e463a079968a",
                "customer": "John",
                "total": 9.2,
                "orderDate": "2020-02-04T06:31:44.190Z",
                "__v": 0
            },
            {
                "_id": "5e390fc33285e463a0799689",
                "customer": "Max",
                "total": 10,
                "orderDate": "2020-02-04T06:31:31.311Z",
                "__v": 0
            }
        ]
    }
]

对于您的情况,您需要发送10作为pageSize值。

您可以使用带有限制和排序的。您可以找到一些示例。@cubrr我想我忘了指定我从REST API访问内容,所以我不能使用游标,因为它需要保存状态。每次处理10个文档时,将批大小为10的游标与所需的任何排序选项一起保存,驱动程序应自动获取下一批数据10@Joe但这样API就不是REST了,因为我必须维护一个状态。也就是说,游标需要保留在服务器上。您可能会使用带有限制和排序的。您可以找到一些示例。@cubrr我想我忘了指定我从REST API访问内容,所以我不能使用游标,因为它需要保存状态。每次处理10个文档时,将批大小为10的游标与所需的任何排序选项一起保存,驱动程序应自动获取下一批数据10@Joe但这样API就不是REST了,因为我必须维护一个状态。也就是说,光标需要保留在服务器上。太棒了!我明天会给你一笔奖金。@GuerlandoOCs我很高兴你喜欢我的答案,接受这个答案对我来说就足够了,你太好了。只是一个问题:在聚合管道中,事情是原子发生的吗?在数据管道的第一步中,它跳过x并限制y。有没有可能,当它跳过那些x时,添加了一个新文档?@GuerlandoOCs问得好,老实说我不知道,但如果我找到答案,我会在这里发表评论。但一般来说,这会很好。因为我们可以计算文档的总数,所以可以使用聚合来创建具有自动增量id的新文档吗?请看我的问题:太棒了!我明天会给你一笔奖金。@GuerlandoOCs我很高兴你喜欢我的答案,接受这个答案对我来说就足够了,你太好了。只是一个问题:在聚合管道中,事情是原子发生的吗?在数据管道的第一步中,它跳过x并限制y。有没有可能,当它跳过那些x时,添加了一个新文档?@GuerlandoOCs问得好,老实说我不知道,但如果我找到答案,我会在这里发表评论。但一般来说,这会很好。因为我们可以计算文档的总数,所以可以使用聚合来创建具有自动增量id的新文档吗?请看我的问题: