如何撤消展开聚合?

时间:2017-06-23 12:20:45

标签: mongodb

考虑该集合是

{ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] }
放松后

{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }

如何将其转换为原始结构?

我希望在不改变结构的情况下获得收藏,但我尝试了群组聚合,它没有提供我所期望的。

实际上我的问题是,在执行放松和分组后我没有获得原始收藏 我的输入集是:

{
    "journeys" : {
        "_id" : "8",
        "originDate" : ISODate("2017-06-27T03:55:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T08:55:00.000Z"),


        "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "35",
                "price" : {
                    "posCurrencyPrice" : "SEK 281.00",
                    "defaultCurrencyPrice" : "SEK 281.00"
                },
                "
                "trainClass" : "1st Class",
                "trainClassCode" : "FIRST",

            }, 
            {
                "_id" : "39",
                "price" : {
                    "posCurrencyPrice" : "SEK 377.00",
                    "defaultCurrencyPrice" : "SEK 377.00"
               },
                "trainClass" : "2nd Class",
                "trainClassCode" : "SECOND",


            }, 

}


{
    "journeys" : {
        "_id" : "10",
        "originDate" : ISODate("2017-06-27T04:20:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T10:50:00.000Z"),

         "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "49",
                "price" : {
                    "posCurrencyPrice" : "SEK 490.00",
                    "defaultCurrencyPrice" : "SEK 490.00"
                },

                "trainClass" : "1st Class",
                "trainClassCode" : "FIRST",



            }, 

    }
}

我想从提案中筛选出第二类 我期待的输出是

{
    "journeys" : {
        "_id" : "8",
        "originDate" : ISODate("2017-06-27T03:55:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T08:55:00.000Z"),


        "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "35",
                "price" : {
                    "posCurrencyPrice" : "SEK 281.00",
                    "defaultCurrencyPrice" : "SEK 281.00"
                },
                "
                "trainClass" : "1st Class",
                "trainClassCode" : "FIRST",

            }, 


}


{
    "journeys" : {
        "_id" : "10",
        "originDate" : ISODate("2017-06-27T04:20:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T10:50:00.000Z"),

         "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "49",
                "price" : {
                    "posCurrencyPrice" : "SEK 490.00",
                    "defaultCurrencyPrice" : "SEK 490.00"
                },

                "trainClass" : "1st Class",
                "trainClassCode" : "FIRST",



            }, 

    }
}

执行以下代码并生成另一个集合

db.searchResource.aggregate({ "$match" : { "_id" :  ObjectId("5951d7217f4a9810ccca7289")}},
{ "$project" : { "_id" : 0 , "journeys" : "$rssSearchResponse.journeys"}} , { "$unwind" : "$journeys"},
{ "$unwind" : "$journeys.proposals"},{ "$match" : { "journeys.proposals.trainClass" : "1st Class"}},
{$group:{"_id":"$journeys._id","originDate": { "$first": "$journeys.originDate" },
"destinationDate": { "$first": "$journeys.destinationDate" },


"dir": { "$first": "$journeys.dir" },
"proposals":{ "$push" : "$journeys.proposals" }}})

输出是:

 {
        "_id" : "8",
        "originDate" : ISODate("2017-06-27T03:55:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T08:55:00.000Z"),


        "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "35",
                "price" : {
                    "posCurrencyPrice" : "SEK 281.00",
                    "defaultCurrencyPrice" : "SEK 281.00"
                },
                "
                "trainClass" : "1st Class",
                "trainClassCode" : "SECOND",

            }, 






        "_id" : "10",
        "originDate" : ISODate("2017-06-27T04:20:00.000Z"),
        "destinationDate" : ISODate("2017-06-27T10:50:00.000Z"),

         "dir" : "OUTBOUND",
        "proposals" : [ 
            {
                "_id" : "49",
                "price" : {
                    "posCurrencyPrice" : "SEK 490.00",
                    "defaultCurrencyPrice" : "SEK 490.00"
                },

                "trainClass" : "1st Class",
                "trainClassCode" : "FIRST",



            }, 


}

1 个答案:

答案 0 :(得分:3)

小组确实是你问题的答案

db.items.aggregate([
    {$match: {}},
    {$unwind: "$sizes"},
    {$group: {
        _id: "$_id",
        sizes: {$push: "$sizes"}
    }}
])