关闭

mongo 中 高级查询之 group

145人阅读 评论(0) 收藏 举报
分类:

数据结构如下

/* 0 */
{
    "_id" : ObjectId("553e04180aec5b607765f34b"),
    "name" : "aaaaaa",
    "date" : ISODate("2015-04-27T09:40:40.345Z"),
    "time" : "",
    "title" : "11313",
    "text" : "121212",
    "pinglun" : [ 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:16.125Z"),
            "text" : "爱的撒旦啊啊",
            "id" : "553e04180aec5b607765f34b"
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("553e03870aec5b607765f34a"),
    "name" : "aaaaaa",
    "date" : ISODate("2015-04-27T09:38:15.409Z"),
    "time" : "",
    "title" : "111",
    "text" : "1111",
    "pinglun" : [ 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-27T09:38:19.356Z"),
            "text" : "11212",
            "id" : "553e03870aec5b607765f34a"
        }, 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:20.410Z"),
            "text" : "爱的撒旦啊啊啊实打实",
            "id" : "553e03870aec5b607765f34a"
        }, 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:23.118Z"),
            "text" : "爱的撒旦啊啊啊实打实",
            "id" : "553e03870aec5b607765f34a"
        }, 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:24.798Z"),
            "text" : "爱的撒旦啊啊啊实打实",
            "id" : "553e03870aec5b607765f34a"
        }, 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:26.295Z"),
            "text" : "爱的撒旦啊啊啊实打实",
            "id" : "553e03870aec5b607765f34a"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("553ee2850aec5b607765f34c"),
    "name" : "aaaaaa",
    "date" : ISODate("2015-04-28T01:29:41.409Z"),
    "time" : "",
    "title" : "12121",
    "text" : "12131313",
    "pinglun" : [ 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:29:46.070Z"),
            "text" : "阿斯达的飒飒",
            "id" : "553ee2850aec5b607765f34c"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("553ee2c80aec5b607765f34e"),
    "name" : "aaaaaa",
    "date" : ISODate("2015-04-28T01:30:48.431Z"),
    "time" : "",
    "title" : "1313",
    "text" : "12121212",
    "pinglun" : []
}

/* 4 */
{
    "_id" : ObjectId("553ee2bf0aec5b607765f34d"),
    "name" : "aaaaaa",
    "date" : ISODate("2015-04-28T01:30:39.398Z"),
    "time" : "",
    "title" : "暗杀暗杀撒啊",
    "text" : "爱上大声的撒",
    "pinglun" : [ 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:30:43.753Z"),
            "text" : "13123131313",
            "id" : "553ee2bf0aec5b607765f34d"
        }, 
        {
            "name" : "aaaaaa",
            "day" : ISODate("2015-04-28T01:32:15.446Z"),
            "text" : "瓦大厦的萨安师大暗杀大师大",
            "id" : "553ee2bf0aec5b607765f34d"
        }
    ]
}

/* 5 */
{
    "_id" : ObjectId("559dda00ed63ee0000dc6548"),
    "name" : "gjc9620",
    "date" : ISODate("2015-07-09T02:18:40.672Z"),
    "time" : "",
    "title" : "adadadada",
    "text" : "adadadasda",
    "pinglun" : []
}

/* 6 */
{
    "_id" : ObjectId("559dda05ed63ee0000dc6549"),
    "name" : "gjc9620",
    "date" : ISODate("2015-07-09T02:18:45.119Z"),
    "time" : "",
    "title" : "adasd",
    "text" : "adasdasdas",
    "pinglun" : []
}

/* 7 */
{
    "_id" : ObjectId("559dda0ced63ee0000dc654a"),
    "name" : "gjc9620",
    "date" : ISODate("2015-07-09T02:18:52.518Z"),
    "time" : "",
    "title" : "asdasd",
    "text" : "afasdasdasdas",
    "pinglun" : []
}

sql如下

db.posts.group({
        "key":{"name":true}, //分组依据
        "initial":{"count":0}, //初始值
        "reduce":function(doc,prev){

            prev.count +=1  //相当于count

        },
        "condition": {"pinglun": {"$gte": {"$size":1}}}  //过滤条件
})

查询结果

/* 0 */
{
    "0" : {
        "name" : "aaaaaa",
        "count" : 4
    },
    "1" : {
        "name" : "gjc9620",
        "count" : 1
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16558次
    • 积分:521
    • 等级:
    • 排名:千里之外
    • 原创:31篇
    • 转载:31篇
    • 译文:0篇
    • 评论:3条