Aggregate Query in Mongodb returns specific field

后端 未结 1 1026
暖寄归人
暖寄归人 2021-02-14 14:26

Document Sample:

{
    \"_id\" : ObjectId(\"53329dfgg43771e49538b4567\"),
    \"u\" : {
        \"_id\" : ObjectId(\"532a435gs4c771edb168c1bd7\"),
        \"n\"          


        
相关标签:
1条回答
  • 2021-02-14 15:11

    The first thing you are doing wrong here is not understanding how $project is intended to work. Pipeline stages such as $project and $group will only output the fields that are "explicitly" identified. So only the fields you say to output will be available to the following pipeline stages.

    Specifically here you "project" only part of the "u" field in your document and you therefore removed the other data from being available. The only present field here now is "name", which is the one you "projected".

    Perhaps it was really your intention to do something like this:

    db.collectiontmp.aggregate([
        { "$group": {
            "_id": {
               "_id": "$u._id",
               "email": { "$toUpper": "$u.e" }
            },
            "total": { "$sum": 1 },
        }},
        { "$project": {
            "_id": 0,
            "email": "$_id.email",
            "total": 1
        }},
        { "$sort": { "total": -1 } },
        { "$limit": 10 }
    ])
    

    Or even:

    db.collectiontmp.aggregate([
        { "$group": {
            "_id": "$u._id",
            "email": { "$first": { "$toUpper": "$u.e" } }
            "total": { "$sum": 1 },
        }},
        { "$project": {
            "_id": 0,
            "email": 1,
            "total": 1
        }},
        { "$sort": { "total": -1 } },
        { "$limit": 10 }
    ])
    

    That gives you the sort of output you are looking for.

    Remember that as this is a "pipeline", then only the "output" from a prior stage is available to the "next" stage. There is no "global" concept of the document as this is not a declarative statement such as in SQL, but a "pipeline".

    So think Unix pipe "|" command, or otherwise look that up. Then your thinking will fall into place.

    0 讨论(0)
提交回复
热议问题