开发者社区> 问答> 正文

mongodb如何对子文档进行分页

文档结构如下:
33
44
55
如何对子文档games进行分页查询
比如一次只取3条数据
66

展开
收起
蛮大人123 2016-02-22 01:20:19 7040 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    > 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获取数组子集更方便一点。

    2019-07-17 18:46:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式 立即下载
阿里云MongoDB云服务构建 立即下载
饿了么高级架构师陈东明:MongoDB是如何逐步提高可靠性的 立即下载