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

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

I have a collection like this:

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


        
相关标签:
2条回答
  • 2021-01-18 19:54

    After a couple of days of thinking/trying various options, this is what I did finally. I modified my document like this:

    {
      "_id" : ObjectId("51f4ad560364f5490ccebe26"),
      "page" : 1,  //1 is the default
      "slug" : "some-unique-string-identifier"
      "fiTpcs" : [
        "uuid1",   //these could be long text, like a long comment/essay
        "uuid2",
        "uuid3",
        "uuid4",
        "uuid5"
      ],
      "fiTpcsCnt" : 5
    }
    

    I keep a "pageCount" and "totalFiTpcsCnt" in memcached. I have set MAX_FITPCSCNT = 500 (500 for now, experimental). When I create a new document of type userext, I set the page value to 1.

    If I have to push a new object to fiTpcs array:

    1) check if "totalFiTpcsCnt" is a multiple of 500. If yes, create a new document of type userext with the same slug, fiTpcsCnt as 0 and fiTpcs array as null. 2) update the last userext - query by slug and "pageCount", push to fiTpcs. Evict cache for "pageCount" and "totalFiTpcsCnt".

    Whenever I need my userext document, I always take just the first page. This way I'll never need to query for more than 500 objects of type fiTpcs at a time and I will still have totalFiTpcsCnt always updated in memcached.

    0 讨论(0)
  • 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 }
    
    0 讨论(0)
提交回复
热议问题