mongodb 中查询子分类的数量
数据结构大概是这样的:
{ "_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"}, }} )
查询出的结果是这样的: