gpt4 book ai didi

mongodb - 如何反转展开聚合?

转载 作者:可可西里 更新时间:2023-11-01 10:02:32 24 4
gpt4 key购买 nike

考虑集合是

{ "_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" }

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

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

实际上我的问题是,在执行 unwind 和 group 后我没有得到原始集合我的输入集合是:

{
"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",



},


}

最佳答案

群确实是你问题的答案

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

关于mongodb - 如何反转展开聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44721309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com