【问题标题】:find specific array from database using mongoose使用猫鼬从数据库中查找特定数组
【发布时间】:2016-01-23 19:17:22
【问题描述】:

我在数据库中有这个 JSON 数据

[
  {

    "total": 30,
    "List": [
      { //data//
        }
       ]
}
]

我需要获取List 数组并跳过其中的项目,但有限制。 如何使用猫鼬查询获取列表数组?

【问题讨论】:

  • fetch the List array and skip the items in it 表示只返回空列表?请在您的问题中添加更多信息。
  • 有限制地跳过
  • 假设此列表中有 30 个项目,您只想返回其中的 10 个?前 10 个还是后 10 个,还是中间 10 个?
  • 是的,Model.find().skip((pageno-1)*5).limit(limit).exec() 正在使用..我只需要从json中获取列表列表
  • 据我所知,limit 只影响Model,不影响子文档...

标签: node.js mongodb mongoose pagination


【解决方案1】:

据我所知,子文档上没有skiplimit的方法。

所以尝试从类似文档中找到List

Model.find({}, 'List', function(err, lists) {

});

lists 包含您想要的所有项目,然后根据需要检索任何项目...

【讨论】:

    【解决方案2】:

    使用$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
    

    猫鼬示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 2021-04-10
      • 2020-09-12
      • 2022-01-26
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2021-01-24
      相关资源
      最近更新 更多