我将如何汇总我的收藏,以便将每个文档除以一个“;“最大值”;mongodb中的聚合?

我将如何汇总我的收藏,以便将每个文档除以一个“;“最大值”;mongodb中的聚合?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,这就是我的文章集中的文档结构 { "_id" : ObjectId("51ed845c92964454ee85fa55"), "_cls" : "Post", "user" : ObjectId("51eb52ba8f24a04aa4761cb8"), "tags" : [ { "tag" : ObjectId("51eb52be8f24a04aa4761cbe")

这就是我的文章集中的文档结构

     {
        "_id" : ObjectId("51ed845c92964454ee85fa55"),
        "_cls" : "Post",
        "user" : ObjectId("51eb52ba8f24a04aa4761cb8"),
        "tags" : [ 
            {
                "tag" : ObjectId("51eb52be8f24a04aa4761cbe"),
                "name" : "rugby"
            }, 
            {
                "tag" : ObjectId("51eb52ba8f24a04aa4761cb8"),
                "name" : "john smith"
            }
        ],
        "content" : "my fourth post!",
        "upvotes" : 1,
        "downvotes" : 0,
        "rank" : 5343.95577,
        "replies" : [],
        "date_created" : ISODate("2013-07-22T15:13:32.650Z"),
        "date_modified" : ISODate("2013-07-22T15:13:32.650Z")
    },
{
    "_id" : ObjectId("51ec9b188f24a04ff0bd8668"),
    "_cls" : "Post",
    "user" : ObjectId("51eb52ba8f24a04aa4761cb8"),
    "tags" : [ 
        {
            "tag" : ObjectId("51eb52be8f24a04aa4761cba"),
            "name" : "rugby"
        }
    ],
    "content" : "http://www.livememe.com/ebejev5 blah blah",
    "upvotes" : 1,
    "downvotes" : 0,
    "replies" : [],
    "date_created" : ISODate("2013-07-21T22:38:16.000Z"),
    "date_modified" : ISODate("2013-07-21T22:38:16.000Z"),
    "rank" : 5000
}
我试图做的是基本上根据每个标签的最高排名来规范排名。 因此,我可以按程序采取以下步骤:

我正试图在mongodb中建立一个聚合。 这是我到目前为止所做的,它只是计算每个标签的排名

db.post.aggregate(    {
        "$unwind" : "$tags"
    },{$group: { _id : {tag: "$tag",rank : "$rank"},"tagName" : {
                "$first" : "$tags"
      }}}, 
     {$group: { _id : {tag: "$tagName.tag"}, trank : {$max : "$_id.rank"}}})
编辑: 好的,我相信我已经弄明白了,仍然需要更多的测试,但似乎有效

db.post.aggregate(
    { "$match" : { "tags" : { "$elemMatch" : { "tag" : { "$in" : [ ObjectId("51eb52ba8f24a04aa4761cb8")]}}}}},
    {$unwind : "$tags"},
    {$group: {_id : "$tags.tag",name :{$first: "$tags.name"},"posts" : {$addToSet : {postid : "$_id", content : "$content",
        rank : "$rank",user : "$user", upvotes : "$upvotes", downvotes : "$downvotes", date_created : "$date_created", date_modified : "$date_modified"
        }},
    "trank" : {$max : "$rank"}}},

    {$unwind : "$posts"},
    {$project:{ _id : "$posts.postid", rank : "$posts.rank",content : "$posts.content",user : "$posts.user", tag : {id :"$_id",name : "$name" },
    upvotes : "$posts.upvotes", downvotes : "$posts.downvotes", date_created : "$posts.date_created", date_modified : "$posts.date_modified",
        erank : {$divide : ["$posts.rank","$trank"]}}},


  //   {$project:{content : 1,tags : 1, _id : 1,erank: 1,posts : 1, tag : 1,rank : 1}},
       {$group : {_id : "$_id","tags" : {$addToSet : { tag : "$tag.id",name: "$tag.name"
        }},"erank" : {$max : "$erank"
        },"content" :{$first : "$content"},"rank" :{$first : "$rank"},"user" :{$first : "$user"},
        upvotes : {$first : "$upvotes"}, downvotes : {$first : "$downvotes"}, date_created : {$first : "$date_created"}
        , date_modified :{$first: "$date_modified"}
        }},
        {$sort : {"erank" : -1,"_id" : 1}},
        {$skip : 0},
        {$limit : 25}


    )
db.post.aggregate(
    { "$match" : { "tags" : { "$elemMatch" : { "tag" : { "$in" : [ ObjectId("51eb52ba8f24a04aa4761cb8")]}}}}},
    {$unwind : "$tags"},
    {$group: {_id : "$tags.tag",name :{$first: "$tags.name"},"posts" : {$addToSet : {postid : "$_id", content : "$content",
        rank : "$rank",user : "$user", upvotes : "$upvotes", downvotes : "$downvotes", date_created : "$date_created", date_modified : "$date_modified"
        }},
    "trank" : {$max : "$rank"}}},

    {$unwind : "$posts"},
    {$project:{ _id : "$posts.postid", rank : "$posts.rank",content : "$posts.content",user : "$posts.user", tag : {id :"$_id",name : "$name" },
    upvotes : "$posts.upvotes", downvotes : "$posts.downvotes", date_created : "$posts.date_created", date_modified : "$posts.date_modified",
        erank : {$divide : ["$posts.rank","$trank"]}}},


  //   {$project:{content : 1,tags : 1, _id : 1,erank: 1,posts : 1, tag : 1,rank : 1}},
       {$group : {_id : "$_id","tags" : {$addToSet : { tag : "$tag.id",name: "$tag.name"
        }},"erank" : {$max : "$erank"
        },"content" :{$first : "$content"},"rank" :{$first : "$rank"},"user" :{$first : "$user"},
        upvotes : {$first : "$upvotes"}, downvotes : {$first : "$downvotes"}, date_created : {$first : "$date_created"}
        , date_modified :{$first: "$date_modified"}
        }},
        {$sort : {"erank" : -1,"_id" : 1}},
        {$skip : 0},
        {$limit : 25}


    )