我在数据库中有这个JSON数据
[
{
"total": 30,
"List": [
{ //data//
}
]
}
]
我需要获取List
数组并跳过其中的项目,但有限制。
如何使用mongoose查询获取列表数组?
答案 0 :(得分:1)
据我所知,子文档上没有skip
或limit
的方法。
因此,请尝试从文档中找到List
,如
Model.find({}, 'List', function(err, lists) {
});
lists
包含您想要的所有项目,然后根据需要检索任何项目......
答案 1 :(得分:1)
使用$slice
进行分页
$slice:[SKIP_VALUE, LIMIT_VALUE]}
{$slice:[0, 3]} //returns only first 3 items
{$slice:[5, 3]} //skip first 5 elements and return next 3 item (6,7,8 no.) items
{$slice:[10, 10]} //return 11-20 no. items
Mongoose示例
Category.find({},{List:{$slice:[5, 3]}}, function(err, limitedItems){
});
这是我的查询输出
{
"_id": "56a34f0ef303a4271073183b",
"categoryid": 40,
"category_name": "find test",
"__v": 0,
"List": [
{
"id": 6,
"name": "a name"
},
{
"id": 7,
"name": "a name"
},
{
"id": 8,
"name": "a name"
}
]
}