ホームページ >バックエンド開発 >PHPチュートリアル >mongodb如何对子文档进行分页
文档结构如下:
<code>{ "_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", } ] }</code>
如何对子文档games进行分页查询
比如一次只取3条数据
<code>{ "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", } }</code>
文档结构如下:
<code>{ "_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", } ] }</code>
如何对子文档games进行分页查询
比如一次只取3条数据
<code>{ "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", } }</code>
<code>> 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" } ] }</code>
取games的前3条数据:
<code>> 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" } ] }</code>
取第四条到第6条数据:
<code>> 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" } ] }</code>
依次类推,即可。 "$slice":[3,3] 第一个3表示查询数组下标的起始位置,第二个3表示取的数据条数。建议games不要过多,不然会超出文档限制16M。不过这个可能是设计问题,我多想了。
第二种方法:就是用代码从数据库中取出来,将games里面的每一个子文档封装成model,放在缓存中做分页,而不是数据库级别的分页也可实现
第三种方法:从数据可看出games里面的子文档是按照id进行排序的,那么也就是说子文档是可比较的,那么就可以使用$gt和$lt,接合$size取数据,我没有试。你可以尝试一下。不过$slice获取数组子集更方便一点。
用limit(n)可以限制只获取n条数据。skip(n)可以跳过n条数据,两者结合可以实现分页吧?不过skip比较消耗资源少用为妙。