如何撤消聚合? [英] How to reverse an unwind aggregation?
本文介绍了如何撤消聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑收藏为
{ "_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" }
如何将其恢复为原始结构?
How to reverse this to original structure?
我想在不更改结构的情况下获取集合,但是我尝试了组聚合,它没有提供我期望的结果.
I want to get collection without changing the structure, But I tried group aggregation, it does not provide what I expected .
实际上我的问题是,展开和分组后我没有得到原始的收藏 我的输入集合是:
Actually my problem was, I didn't get original collection after performing unwind and group my input collection was :
{
"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",
},
}
}
我想从提案中过滤掉第二类 我期望的输出是
I want to filter out 2nd class from the proposals My expecting output is
{
"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",
},
}
}
执行以下代码,并生成另一个集合
performed following code and results another collection
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",
},
}
推荐答案
分组确实是您问题的答案
Group is indeed the answer to your question
db.items.aggregate([
{$match: {}},
{$unwind: "$sizes"},
{$group: {
_id: "$_id",
sizes: {$push: "$sizes"}
}}
])
这篇关于如何撤消聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文