How to get paginated/sliced data of subdocument array in mongo collection?

前端 未结 2 901
难免孤独
难免孤独 2021-01-18 19:08

I have a collection like this:

{
\"_id\" : ObjectId(\"51f4ad560364f5490ccebe26\"),
\"fiTpcs\" : [
    \"uuid1\",
    \"uuid2\",
    \"uuid3\",
    \"uuid4\",         


        
2条回答
  •  温柔的废话
    2021-01-18 20:04

    I may not understand your question in full depth, but seems like $slice is the droid your are looking for:

    > db.page.find()
    { "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid1", "uuid2", "uuid3", "uuid4", "uuid5" ], "fiTpcsCnt" : 2 }
    > db.page.find({}, {"fiTpcs" : {$slice : 3}})
    { "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid1", "uuid2", "uuid3" ], "fiTpcsCnt" : 2 }
    > db.page.find({}, {"fiTpcs" : {$slice : [1,3]}})
    { "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid2", "uuid3", "uuid4" ], "fiTpcsCnt" : 2 }
    

提交回复
热议问题