mongo DB中的列表级别分页

时间:2017-01-31 09:19:10

标签: mongodb

Mongo文档结构如下:

{ 
    "dictID" : "dd001", 
    "dictVersion" : 1, 
    "addMeasures" : 
    [ 
        { 
            "measureId" : "f229ba18", 
            "userId" : "3966C3DD", 
            "created" : "2017-01-07T05:47:22.512Z" 
        }, { 
            "measureId" : "0b701469", 
            "userId" : "3966C3DD", 
            "created" : "2017-01-07T05:47:35.193Z" 
        } , { 
            "measureId" : "f229ba1823", 
            "userId" : "3966C3DD", 
            "created" : "2017-01-07T05:47:22.512Z" 
        }, { 
            "measureId" : "0b7014699", 
            "userId" : "3966C3DD", 
            "created" : "2017-01-07T05:47:35.193Z" 
        } 
    ]
}

我需要对数组列表中的单个文档进行分页

实施例。在我的文档中“addMeasures”有多个地图,我想要分页,这样它就可以给我一些“addMeasures”,比如限制1,2。

1 个答案:

答案 0 :(得分:1)

使用$slice运算符

db.collection.find({}, {dictId: 1, dictVersion: 1, addMeasures: {$slice: [0, 2]}})

返回:

{
   "_id":ObjectId("5890572371cbc5eae2c9535b"),
   "dictVersion":1,
   "addMeasures":[
      {
         "measureId":"f229ba18",
         "userId":"3966C3DD",
         "created":"2017-01-07T05:47:22.512Z"
      },
      {
         "measureId":"0b701469",
         "userId":"3966C3DD",
         "created":"2017-01-07T05:47:35.193Z"
      }
   ]
}