consider the collection was
{ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] }
after unwind
{ "_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" }}})
Output was :
{
"_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"}
}}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With