Aggregate/project sub-document as top-level document in mongo

后端 未结 3 1922
臣服心动
臣服心动 2021-02-13 13:36

After a few aggregation steps (pipeline steps) in one of my collections, I\'m ending up with the following result:

{
    \"_id\" : ObjectId(\"574e7722bffe901713d         


        
3条回答
  •  臣服心动
    2021-02-13 13:56

    As you have guessed, $project allows you to do that:

    db.col.aggregate([
    {
        $project : 
        {
            _id: "$command._id",
            name: "$command.name", 
            strike: "$command.strike", 
            duration: "$command.duration"
        }
    }
    ]).pretty()
    

    I inserted your previous results and the above query returned this:

    {
        "_id" : ObjectId("57ec6b6f6c61e919b578fe7c"),
        "name" : "Run",
        "strike" : 15,
        "duration" : 123
    }
    {
        "_id" : ObjectId("573d688d080cc2cbe8aecbbc"),
        "name" : "Run",
        "strike" : 12,
        "duration" : 597
    }
    

    So piping your query with this $product should produce the result you are looking for.

    Update after comments

    If the exact structure is not your main concern, but rather the exclusion of few fields (wihtout having to list all fields to include), then you may use find() instead of aggregate().

    aggregate's product only lets you exclude _id. This means you need to manually list all fields to include.
    Note: Since version 3.4 of MongoDB it is possible to exclude fields in $project phase (https://docs.mongodb.com/manual/reference/operator/aggregation/project/#exclude-fields)

    find however, lets you list the fields to hide.

    Alternative

    (1) You could redirect your aggregate result to another collection using $out :

    { $out : "commands" }
    

    (2) Even though the structure won't be exactly as you'd like, you'll then be able to do a find query and hide fields:

    db.commands.find({}, {_id:0, "command.score":0, eventname:0}).pretty()
    

    It returns this, which is pretty close to what you were looking for:

    {
        "command" : {
            "_id" : ObjectId("57ec6b6f6c61e919b578fe7c"),
            "name" : "Run",
            "strike" : 15,
            "duration" : 123
        }
    }
    

提交回复
热议问题