mongodb 中查询子分类的数量

让人想犯罪 __ 提交于 2020-02-29 18:09:35

数据结构大概是这样的:

{
    "_id" : ObjectId("5cbd9384ec422c7a39cfb4d4"),
    "city" : [ 
        {
            "count" : NumberLong(1),
            "rank" : NumberLong(1),
            "score" : NumberLong(0),
            "tagid" : "北京",
            "tagone" : "0",
            "tagthree" : "0",
            "tagtwo" : "0",
            "tagtype" : NumberLong(5),
            "username" : "v2716770"
        }
    ],
    "industry" : [],
    "power" : [ 
        {
            "count" : NumberLong(100),
            "rank" : NumberLong(1),
            "score" : NumberLong(10000000),
            "tagid" : "5",
            "tagone" : "0",
            "tagthree" : "0",
            "tagtwo" : "0",
            "tagtype" : NumberLong(4),
            "username" : "v2716770"
        }, 
        {
            "count" : NumberLong(1),
            "rank" : NumberLong(2),
            "score" : NumberLong(8),
            "tagid" : "2",
            "tagone" : "0",
            "tagthree" : "0",
            "tagtwo" : "0",
            "tagtype" : NumberLong(4),
            "username" : "v2716770"
        }
    ],
    "produline" : [ 
        {
            "count" : NumberLong(1),
            "rank" : NumberLong(1),
            "score" : NumberLong(8),
            "tagid" : "3",
            "tagone" : "0",
            "tagthree" : "0",
            "tagtwo" : "0",
            "tagtype" : NumberLong(3),
            "username" : "v2716770"
        }
    ],
    "tenet" : [],
    "username" : "v2716770"
}

 查询每个子分类下的数组长度;

db.taguser.aggregate(
    { "$project" : {
        "username" : 1 ,
        "tenet" : {$size:"$tenet"}, 
        "industry" : {$size:"$industry"}, 
        "power" : {$size:"$power"}, 
        "produline" : {$size:"$produline"}, 
        "city" : {$size:"$city"}, 
    }}
 )

查询出的结果是这样的:

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!