mongodb如何對子文檔進行分頁

來源:互聯網
上載者:User
文檔結構如下:

{    "_id" : ObjectId("55dc145ef754a3342000002d"),    "id" : 12,    "title" : "炎炎夏日,跟我一起High翻天!",    "content" : "酷暑7月,簡直不能忍~~~!夏天。就應該是被西瓜,冰鎮可樂,全家桶,空調和遊戲包圍著的天堂。外面的世界紛紛擾擾與我無關~~哇哈哈哈!!",    "thumb" : "/201506/5592560017b6a.jpg",    "banner" : "/201506/559255fa5366d.jpg",    "order" : 11,    "publish_time" : "2015-08-25",    "game_num" : 14,    "games" : [         {            "id" : 1128,            "name_zh" : "追擊野獸",            "apk_size" : "38.15M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "com.miniclip.beastquest",            "apk_version_code" : "18",            "game_rank" : 8,            "customize_update_time" : "2015-05-18 00:00:00",        },         {            "id" : 3276,            "name_zh" : "世界殭屍大聯盟",            "apk_size" : "33.88M",            "apk_version_name" : "3.3.3237",            "apk_package_name" : "com.proletariat.worldzombination",            "apk_version_code" : "33537",            "game_rank" : 8,            "customize_update_time" : "2015-05-15 00:00:00",        },         {            "id" : 3416,            "name_zh" : "圍剿外星客",            "apk_size" : "38.69M",            "apk_version_name" : "1.7.1",            "apk_package_name" : "com.outplayentertainment.aliencreeps",            "apk_version_code" : "10070010",            "game_rank" : 8,            "customize_update_time" : "2015-04-25 00:00:00",        },         {            "id" : 10357,            "name_zh" : "合金彈頭:防禦",            "apk_size" : "47.13M",            "apk_version_name" : "1.27.0",            "apk_package_name" : "com.snkplaymore.android003",            "apk_version_code" : "52",            "game_rank" : 8,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10360,            "name_zh" : "綠色忍者蛙年",            "apk_size" : "47.7M",            "apk_version_name" : "4",            "apk_package_name" : "com.nitrome.greenninja",            "apk_version_code" : "4",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 15:52:10",        },         {            "id" : 10364,            "name_zh" : "殺死馬里奧",            "apk_size" : "31.16M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "air.com.keybol.plumber",            "apk_version_code" : "1000006",            "game_rank" : 7,            "customize_update_time" : "2015-06-30 16:08:09",         },         {            "id" : 10366,            "name_zh" : "通過繁殖征服世界",            "apk_size" : "44.83M",            "apk_version_name" : "1.0.2",            "apk_package_name" : "com.kt.SpermWarWorldWide_G",            "apk_version_code" : "10002",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:21:22",         },         {            "id" : 10229,            "name_zh" : "冰塊切割",            "apk_size" : "7.14M",            "apk_version_name" : "2.4.6",            "apk_package_name" : "com.hyperkani.sliceice",            "apk_version_code" : "30",            "game_rank" : 7,            "customize_update_time" : "2014-11-04 00:00:00",        },         {            "id" : 10356,            "name_zh" : "吃冰淇淋的怪房子",            "apk_size" : "33.56M",            "apk_version_name" : "1.0.2",            "apk_package_name" : "com.firedroid.icecreamnomsters",            "apk_version_code" : "4",            "game_rank" : 8,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10358,            "name_zh" : "來杯果汁",            "apk_size" : "42.11M",            "apk_version_name" : "1.6.21",            "apk_package_name" : "air.com.sgn.juicejam.gp",            "apk_version_code" : "1006021",            "game_rank" : 6,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10362,            "name_zh" : "夢遊先生",            "apk_size" : "48.52M",            "apk_version_name" : "1.00",            "apk_package_name" : "com.BEbit.MrSnoozleberg",            "apk_version_code" : "1",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 15:58:13",        },         {            "id" : 10363,            "name_zh" : "清涼方冰冰",            "apk_size" : "23.14M",            "apk_version_name" : "1.1",            "apk_package_name" : "com.unilever.CoolCubeszzc",            "apk_version_code" : "2",            "game_rank" : 6,            "customize_update_time" : "2015-06-30 16:04:56",        },         {            "id" : 10365,            "name_zh" : "嗜血狂鯊2",            "apk_size" : "9.9M",            "apk_version_name" : "2.6.1",            "apk_package_name" : "com.fgol.shark2",            "apk_version_code" : "13",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:15:58",        },         {            "id" : 10367,            "name_zh" : "外星人愛牛奶",            "apk_size" : "23.86M",            "apk_version_name" : "1.0.7",            "apk_package_name" : "air.com.rightfusion.alienslikemilk",            "apk_version_code" : "1000007",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:28:17",        }    ]}

如何對子文檔games進行分頁查詢
比如一次只取3條資料

{            "id" : 1128,            "name_zh" : "追擊野獸",            "apk_size" : "38.15M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "com.miniclip.beastquest",            "apk_version_code" : "18",            "game_rank" : 8,            "customize_update_time" : "2015-05-18 00:00:00",        },         {            "id" : 3276,            "name_zh" : "世界殭屍大聯盟",            "apk_size" : "33.88M",            "apk_version_name" : "3.3.3237",            "apk_package_name" : "com.proletariat.worldzombination",            "apk_version_code" : "33537",            "game_rank" : 8,            "customize_update_time" : "2015-05-15 00:00:00",        },         {            "id" : 3416,            "name_zh" : "圍剿外星客",            "apk_size" : "38.69M",            "apk_version_name" : "1.7.1",            "apk_package_name" : "com.outplayentertainment.aliencreeps",            "apk_version_code" : "10070010",            "game_rank" : 8,            "customize_update_time" : "2015-04-25 00:00:00",        }}

回複內容:

文檔結構如下:

{    "_id" : ObjectId("55dc145ef754a3342000002d"),    "id" : 12,    "title" : "炎炎夏日,跟我一起High翻天!",    "content" : "酷暑7月,簡直不能忍~~~!夏天。就應該是被西瓜,冰鎮可樂,全家桶,空調和遊戲包圍著的天堂。外面的世界紛紛擾擾與我無關~~哇哈哈哈!!",    "thumb" : "/201506/5592560017b6a.jpg",    "banner" : "/201506/559255fa5366d.jpg",    "order" : 11,    "publish_time" : "2015-08-25",    "game_num" : 14,    "games" : [         {            "id" : 1128,            "name_zh" : "追擊野獸",            "apk_size" : "38.15M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "com.miniclip.beastquest",            "apk_version_code" : "18",            "game_rank" : 8,            "customize_update_time" : "2015-05-18 00:00:00",        },         {            "id" : 3276,            "name_zh" : "世界殭屍大聯盟",            "apk_size" : "33.88M",            "apk_version_name" : "3.3.3237",            "apk_package_name" : "com.proletariat.worldzombination",            "apk_version_code" : "33537",            "game_rank" : 8,            "customize_update_time" : "2015-05-15 00:00:00",        },         {            "id" : 3416,            "name_zh" : "圍剿外星客",            "apk_size" : "38.69M",            "apk_version_name" : "1.7.1",            "apk_package_name" : "com.outplayentertainment.aliencreeps",            "apk_version_code" : "10070010",            "game_rank" : 8,            "customize_update_time" : "2015-04-25 00:00:00",        },         {            "id" : 10357,            "name_zh" : "合金彈頭:防禦",            "apk_size" : "47.13M",            "apk_version_name" : "1.27.0",            "apk_package_name" : "com.snkplaymore.android003",            "apk_version_code" : "52",            "game_rank" : 8,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10360,            "name_zh" : "綠色忍者蛙年",            "apk_size" : "47.7M",            "apk_version_name" : "4",            "apk_package_name" : "com.nitrome.greenninja",            "apk_version_code" : "4",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 15:52:10",        },         {            "id" : 10364,            "name_zh" : "殺死馬里奧",            "apk_size" : "31.16M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "air.com.keybol.plumber",            "apk_version_code" : "1000006",            "game_rank" : 7,            "customize_update_time" : "2015-06-30 16:08:09",         },         {            "id" : 10366,            "name_zh" : "通過繁殖征服世界",            "apk_size" : "44.83M",            "apk_version_name" : "1.0.2",            "apk_package_name" : "com.kt.SpermWarWorldWide_G",            "apk_version_code" : "10002",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:21:22",         },         {            "id" : 10229,            "name_zh" : "冰塊切割",            "apk_size" : "7.14M",            "apk_version_name" : "2.4.6",            "apk_package_name" : "com.hyperkani.sliceice",            "apk_version_code" : "30",            "game_rank" : 7,            "customize_update_time" : "2014-11-04 00:00:00",        },         {            "id" : 10356,            "name_zh" : "吃冰淇淋的怪房子",            "apk_size" : "33.56M",            "apk_version_name" : "1.0.2",            "apk_package_name" : "com.firedroid.icecreamnomsters",            "apk_version_code" : "4",            "game_rank" : 8,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10358,            "name_zh" : "來杯果汁",            "apk_size" : "42.11M",            "apk_version_name" : "1.6.21",            "apk_package_name" : "air.com.sgn.juicejam.gp",            "apk_version_code" : "1006021",            "game_rank" : 6,            "customize_update_time" : "0000-00-00 00:00:00",        },         {            "id" : 10362,            "name_zh" : "夢遊先生",            "apk_size" : "48.52M",            "apk_version_name" : "1.00",            "apk_package_name" : "com.BEbit.MrSnoozleberg",            "apk_version_code" : "1",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 15:58:13",        },         {            "id" : 10363,            "name_zh" : "清涼方冰冰",            "apk_size" : "23.14M",            "apk_version_name" : "1.1",            "apk_package_name" : "com.unilever.CoolCubeszzc",            "apk_version_code" : "2",            "game_rank" : 6,            "customize_update_time" : "2015-06-30 16:04:56",        },         {            "id" : 10365,            "name_zh" : "嗜血狂鯊2",            "apk_size" : "9.9M",            "apk_version_name" : "2.6.1",            "apk_package_name" : "com.fgol.shark2",            "apk_version_code" : "13",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:15:58",        },         {            "id" : 10367,            "name_zh" : "外星人愛牛奶",            "apk_size" : "23.86M",            "apk_version_name" : "1.0.7",            "apk_package_name" : "air.com.rightfusion.alienslikemilk",            "apk_version_code" : "1000007",            "game_rank" : 8,            "customize_update_time" : "2015-06-30 16:28:17",        }    ]}

如何對子文檔games進行分頁查詢
比如一次只取3條資料

{            "id" : 1128,            "name_zh" : "追擊野獸",            "apk_size" : "38.15M",            "apk_version_name" : "1.0.6",            "apk_package_name" : "com.miniclip.beastquest",            "apk_version_code" : "18",            "game_rank" : 8,            "customize_update_time" : "2015-05-18 00:00:00",        },         {            "id" : 3276,            "name_zh" : "世界殭屍大聯盟",            "apk_size" : "33.88M",            "apk_version_name" : "3.3.3237",            "apk_package_name" : "com.proletariat.worldzombination",            "apk_version_code" : "33537",            "game_rank" : 8,            "customize_update_time" : "2015-05-15 00:00:00",        },         {            "id" : 3416,            "name_zh" : "圍剿外星客",            "apk_size" : "38.69M",            "apk_version_name" : "1.7.1",            "apk_package_name" : "com.outplayentertainment.aliencreeps",            "apk_version_code" : "10070010",            "game_rank" : 8,            "customize_update_time" : "2015-04-25 00:00:00",        }}

> db.test1.find().pretty(){    "_id" : ObjectId("55dc145ef754a3342000002d"),    "games" : [        {            "id" : 1128,            "name_zh" : "追擊野獸",            "customize_update_time" : "2015-05-18 00:00:00"        },        {            "id" : 3276,            "name_zh" : "dasda",            "customize_update_time" : "2015-05-15 00:00:00"        },        {            "id" : 3416,            "name_zh" : "圍剿外星客",            "customize_update_time" : "2015-04-25 00:00:00"        },        {            "id" : 10357,            "name_zh" : "合金彈頭:防禦",            "customize_update_time" : "0000-00-00 00:00:00"        },        {            "id" : 10360,            "name_zh" : "綠色忍者蛙年",            "customize_update_time" : "2015-06-30 15:52:10"        },        {            "id" : 10364,            "name_zh" : "殺死馬里奧",            "customize_update_time" : "2015-06-30 16:08:09"        },        {            "id" : 10366,            "name_zh" : "通過繁殖征服世界",            "customize_update_time" : "2015-06-30 16:21:22"        },        {            "id" : 10229,            "name_zh" : "冰塊切割",            "customize_update_time" : "2014-11-04 00:00:00"        },        {            "id" : 10356,            "name_zh" : "吃冰淇淋的怪房子",            "customize_update_time" : "0000-00-00 00:00:00"        },        {            "id" : 10358,            "name_zh" : "來杯果汁",            "customize_update_time" : "0000-00-00 00:00:00"        },        {            "id" : 10362,            "name_zh" : "夢遊先生",            "customize_update_time" : "2015-06-30 15:58:13"        },        {            "id" : 10363,            "name_zh" : "清涼方冰冰",            "customize_update_time" : "2015-06-30 16:04:56"        },        {            "id" : 10365,            "name_zh" : "嗜血狂鯊2",            "customize_update_time" : "2015-06-30 16:15:58"        },        {            "id" : 10367,            "name_zh" : "外星人愛牛奶",            "customize_update_time" : "2015-06-30 16:28:17"        }    ]}

取games的前3條資料:

> db.test1.find({"_id" : ObjectId("55dc145ef754a3342000002d")},{"games":{ "$slice":[0,3]}}).pretty(){    "_id" : ObjectId("55dc145ef754a3342000002d"),    "games" : [        {            "id" : 1128,            "name_zh" : "追擊野獸",            "customize_update_time" : "2015-05-18 00:00:00"        },        {            "id" : 3276,            "name_zh" : "dasda",            "customize_update_time" : "2015-05-15 00:00:00"        },        {            "id" : 3416,            "name_zh" : "圍剿外星客",            "customize_update_time" : "2015-04-25 00:00:00"        }    ]}

取第四條到第6條資料:

> db.test1.find({"_id" : ObjectId("55dc145ef754a3342000002d")},{"games":{ "$slice":[3,3]}}).pretty(){    "_id" : ObjectId("55dc145ef754a3342000002d"),    "games" : [        {            "id" : 10357,            "name_zh" : "合金彈頭:防禦",            "customize_update_time" : "0000-00-00 00:00:00"        },        {            "id" : 10360,            "name_zh" : "綠色忍者蛙年",            "customize_update_time" : "2015-06-30 15:52:10"        },        {            "id" : 10364,            "name_zh" : "殺死馬里奧",            "customize_update_time" : "2015-06-30 16:08:09"        }    ]}

依次類推,即可。 "$slice":[3,3] 第一個3表示查詢數組下標的起始位置,第二個3表示取的資料條數。建議games不要過多,不然會超出文檔限制16M。不過這個可能是設計問題,我多想了。

第二種方法:就是用代碼從資料庫中取出來,將games裡面的每一個子文檔封裝成model,放在緩衝中做分頁,而不是資料庫層級的分頁也可實現

第三種方法:從資料可看出games裡面的子文檔是按照id進行排序的,那麼也就是說子文檔是可比較的,那麼就可以使用$gt和$lt,接合$size取資料,我沒有試。你可以嘗試一下。不過$slice擷取數組子集更方便一點。

用limit(n)可以限制只擷取n條資料。skip(n)可以跳過n條資料,兩者結合可以實現分頁吧?不過skip比較消耗資源少用為妙。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.