为什么 mongodb aggreate $unwind更改我的$sort顺序?



我有一个像这样的mongodb(4.2(聚合函数:

Wine.aggregate(
[
{
"$match": {
"user": ObjectId("<userId>"),
"deleted": false,
"$or": [
{
"mode": "past"
},
{
"mode": "group"
},
{
"$and": [
{
"mode":"event"
},
{
"sameAs": {
"$exists": true
}
}
]
}
]
}
},
{
"$sort": {
"created": -1
}
},
{
"$lookup": {
"from": "wineuniversals", // the collection name
"localField": "wineUniversal", // field from the wines model
"foreignField": "_id", // field how the two collections are linked
"as": "uWineData" // the object property where the universal wine data is stored
}
},
{
"$unwind": {
"path": "$uWineData"
}
}
]
).exec(function(err, wines) {...});

不带$unwind的示例输出:(返回 5 个文档(

[{"_id":"5ed5b3d186bff9019b43d173","scoreAroma":4.5,"scoreTaste":4.5,"scoreFinish":4.3,"scoreOverallImpression":4.4,"scoreTotal":94.3,"comment":"Amazingly delicious!","groupDescription":"none","quantity":1,"eventBlindTasting":false,"deleted":false,"hidden":false,"wineUniversal":null,"created":"2020-06-05T03:18:09.428Z","mode":"past","lastUpdated":"2020-06-05T03:18:09.428Z","user":"554f99352ee62248071b4d0f","photoURL":"00d28cb99a8460816e0c079cea8f9f45","__v":0,"aromaTags":{"Intensity":{"values":["Powerful"]},"Fruit":{"values":["Apple","Pear","Pitted Fruit","Tropical"]},"Fruit Character":{"values":["Baked","Ripe"]},"Oak Intensity":{"values":["None"]}},"finishTags":{"Length/Finish":{"values":["Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Complex"]}},"tasteTags":{"Sweetness":{"values":["Off Dry"]},"Tannins":{"values":["Low"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderately Low"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Citrus","Tropical"]},"Fruit Character":{"values":["Peel/Skin","Tart","Ripe","Fresh"]},"Non-Fruit":{"values":["Floral","Vegetal"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["Some"]},"Color":{"values":["Straw"]},"Color Intensity":{"values":["Pale"]}},"uWineData":[]},{"_id":"5ed081414df3ac537644e827","scoreAroma":4,"scoreTaste":4.4,"scoreFinish":4.2,"scoreOverallImpression":4.2,"scoreTotal":92.3,"comment":"comment: none","groupDescription":"2017 Domaine Jean Royer, Chateauneuf du Pape","quantity":1,"eventBlindTasting":false,"deleted":false,"hidden":false,"wineUniversal":null,"created":"2020-05-27T20:48:49.149Z","eventLink":"5ecec8b7e3b963049276ecfc","mode":"event","eventListOrder":0,"lastUpdated":"2020-05-29T03:28:50.506Z","photoURL":"fd7f45af94457ec9e575f62fe1fc32b0","__v":0,"sameAs":"5eced200e3b963049276ed05","visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Lot"]},"Color":{"values":["Garnet"]},"Color Intensity":{"values":["Deep"]}},"aromaTags":{"Intensity":{"values":["Delicate"]},"Fruit":{"values":["Black Fruit"]},"Fruit Character":{"values":["Ripe","Fresh","Jammy"]},"Non-Fruit":{"values":["Licorice","Honey"]},"Organic/Inorganic":{"values":["Fresh Soil"]},"Oak Intensity":{"values":["Low"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderate"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderately High"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Citrus","Red Fruit","Black Fruit"]},"Fruit Character":{"values":["Peel/Skin","Ripe"]},"Non-Fruit":{"values":["Spice","Licorice","Tobacco"]},"Organic/Inorganic":{"values":["Forest Floor","Fresh Soil"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Complex"]}},"user":"554f99352ee62248071b4d0f","uWineData":[]},{"_id":"5dbbc408e78d867664213147","photoURL":"51972ee99dec8f31cdb6ff8025a0d3d3","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dce6038e78d8676642131fd","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"Surprisingly tasty. You'd think it's be olonk based on the gimmicky label.","scoreTotal":91,"scoreOverallImpression":4.2,"scoreFinish":4,"scoreTaste":4,"scoreAroma":4.3,"lastUpdated":"2020-05-21T23:06:54.497Z","created":"2020-05-21T23:06:54.498Z","__v":0,"aromaTags":{"Intensity":{"values":["Powerful"]},"Fruit":{"values":["Red Fruit","Black Fruit"]},"Fruit Character":{"values":["Peel/Skin","Ripe","Jammy"]},"Non-Fruit":{"values":["Wood","Tobacco"]},"Oak Intensity":{"values":["Low"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderately Low"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderate"]},"Body/Texture":{"values":["Medium"]},"Fruit":{"values":["Citrus","Red Fruit","Black Fruit"]},"Fruit Character":{"values":["Peel/Skin","Fresh","Jammy"]},"Non-Fruit":{"values":["Spice","Wood","Tobacco"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Dull"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["Some"]},"Color":{"values":["Garnet"]},"Color Intensity":{"values":["Deep"]}},"uWineData":[{"_id":"5dce6038e78d8676642131fd","scoreCount":1,"averageScore":0,"userWines":["5dbbc408e78d867664213147"],"expertScore2":"","expertReviewer2":null,"expertScore1":"","expertReviewer1":null,"currency":"USD","commonPrice":12,"additionalDetails":"","designation":"","category":"Red","varietal":"Cabernet Sauvignon","vineyard":"","appellation":"California","subRegion":"","region":"California","country":"United States","wineryUrl":"https://www.thewalkingdeadwine.com","winery":"The Walking Dead Wines","vintage":"2016","deleted":false,"lastUpdated":"2019-11-15T08:22:32.437Z","created":"2019-11-15T08:22:16.579Z","__v":1}]},{"_id":"5dbe0233e78d86766421317b","photoURL":"64132a46cceddb94d223dafcd38dc572","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dce97cfe78d867664213236","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"This wine has been great with every vintage I've tried. Balanced, complex, delicious red fruit bright acidity with enough fruit to balance it out. Light tannins and just a touch of oaky spice. Wonderful!!","scoreTotal":93.7,"scoreOverallImpression":4.4,"scoreFinish":4.2,"scoreTaste":4.4,"scoreAroma":4.5,"lastUpdated":"2020-05-19T23:58:47.219Z","created":"2020-05-19T23:58:47.220Z","__v":0,"aromaTags":{"Intensity":{"values":["Powerful"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Peel/Skin","Ripe","Fresh"]},"Non-Fruit":{"values":["Herbal","Spice","Wood"]},"Organic/Inorganic":{"values":["Stone"]},"Oak Intensity":{"values":["Low"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderate"]},"Acid":{"values":["Moderate"]},"Alcohol":{"values":["Moderately Low"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Peel/Skin","Tart","Fresh"]},"Non-Fruit":{"values":["Herbal","Spice","Licorice","Wood"]},"Organic/Inorganic":{"values":["Stone"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Few"]},"Color":{"values":["Ruby"]},"Color Intensity":{"values":["Medium"]}},"uWineData":[{"_id":"5dce97cfe78d867664213236","scoreCount":1,"averageScore":95.1,"userWines":["5dbe0233e78d86766421317b","5dfea3bde78d8676642135f4"],"expertScore2":"","expertReviewer2":"none","expertScore1":"","expertReviewer1":"none","currency":"USD","commonPrice":14,"additionalDetails":"","designation":"Firehouse Red","category":"Red","varietal":"Red Blend","vineyard":"","appellation":"Columbia Valley","subRegion":"Columbia Valley","region":"Washington","country":"United States","wineryUrl":"http://www.tamarackcellars.com","winery":"Tamarack cellars","vintage":"2016","deleted":false,"lastUpdated":"2019-12-21T23:15:06.578Z","created":"2019-11-15T12:19:27.348Z","__v":2}]},{"_id":"5df533bee78d8676642134d4","photoURL":"077d87cb7aac91cc59f3a0cf1f981197","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dfb322fe78d867664213582","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"Moderately big wine. Heavy acidity, tartness, and with a great red and blue fruit aromas.","scoreTotal":90.9,"scoreOverallImpression":4.1,"scoreFinish":3.9,"scoreTaste":4.1,"scoreAroma":4.3,"lastUpdated":"2020-05-14T22:31:13.806Z","created":"2020-05-14T22:31:13.807Z","__v":0,"aromaTags":{"Intensity":{"values":["Moderate"]},"Fruit":{"values":["Red Fruit","Blue Fruit"]},"Fruit Character":{"values":["Ripe","Jammy"]},"Non-Fruit":{"values":["Floral","Herbal","Spice"]},"Organic/Inorganic":{"values":["Stone"]},"Oak Intensity":{"values":["None"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["No"]},"Balance: If No, Which of the Following Elements Stick Out?":{"values":["Acidity","Tannin"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderately High"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderately Low"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Citrus","Red Fruit","Blue Fruit"]},"Fruit Character":{"values":["Tart","Ripe","Fresh"]},"Non-Fruit":{"values":["Herbal","Wood"]},"Organic/Inorganic":{"values":["Stone","Petrol"]}},"visualTags":{"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Few"]},"Color":{"values":["Ruby"]},"Color Intensity":{"values":["Deep"]}},"uWineData":[{"_id":"5dfb322fe78d867664213582","scoreCount":1,"averageScore":0,"userWines":["5df533bee78d8676642134d4"],"expertScore2":"","expertReviewer2":null,"expertScore1":"","expertReviewer1":null,"currency":"USD","commonPrice":30,"additionalDetails":"","designation":"","category":"Red","varietal":"Red Bordeaux Blend","vineyard":"","appellation":"Saint-Emilion Grand Cru","subRegion":"Saint-Emilion","region":"Bordeaux","country":"France","wineryUrl":"","winery":"Chateau Patris Querre","vintage":"2016","deleted":false,"lastUpdated":"2019-12-19T08:18:00.075Z","created":"2019-12-19T08:17:51.237Z","__v":1}]}]

下面是将$unwind步骤添加到我的聚合管道的示例输出:(返回 5 个文档(

[{"_id":"5dbbc408e78d867664213147","photoURL":"51972ee99dec8f31cdb6ff8025a0d3d3","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dce6038e78d8676642131fd","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"Surprisingly tasty. You'd think it's be olonk based on the gimmicky label.","scoreTotal":91,"scoreOverallImpression":4.2,"scoreFinish":4,"scoreTaste":4,"scoreAroma":4.3,"lastUpdated":"2020-05-21T23:06:54.497Z","created":"2020-05-21T23:06:54.498Z","__v":0,"aromaTags":{"Intensity":{"values":["Powerful"]},"Fruit":{"values":["Red Fruit","Black Fruit"]},"Fruit Character":{"values":["Peel/Skin","Ripe","Jammy"]},"Non-Fruit":{"values":["Wood","Tobacco"]},"Oak Intensity":{"values":["Low"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderately Low"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderate"]},"Body/Texture":{"values":["Medium"]},"Fruit":{"values":["Citrus","Red Fruit","Black Fruit"]},"Fruit Character":{"values":["Peel/Skin","Fresh","Jammy"]},"Non-Fruit":{"values":["Spice","Wood","Tobacco"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Dull"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["Some"]},"Color":{"values":["Garnet"]},"Color Intensity":{"values":["Deep"]}},"uWineData":{"_id":"5dce6038e78d8676642131fd","scoreCount":1,"averageScore":0,"userWines":["5dbbc408e78d867664213147"],"expertScore2":"","expertReviewer2":null,"expertScore1":"","expertReviewer1":null,"currency":"USD","commonPrice":12,"additionalDetails":"","designation":"","category":"Red","varietal":"Cabernet Sauvignon","vineyard":"","appellation":"California","subRegion":"","region":"California","country":"United States","wineryUrl":"https://www.thewalkingdeadwine.com","winery":"The Walking Dead Wines","vintage":"2016","deleted":false,"lastUpdated":"2019-11-15T08:22:32.437Z","created":"2019-11-15T08:22:16.579Z","__v":1}},{"_id":"5dbe0233e78d86766421317b","photoURL":"64132a46cceddb94d223dafcd38dc572","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dce97cfe78d867664213236","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"This wine has been great with every vintage I've tried. Balanced, complex, delicious red fruit bright acidity with enough fruit to balance it out. Light tannins and just a touch of oaky spice. Wonderful!!","scoreTotal":93.7,"scoreOverallImpression":4.4,"scoreFinish":4.2,"scoreTaste":4.4,"scoreAroma":4.5,"lastUpdated":"2020-05-19T23:58:47.219Z","created":"2020-05-19T23:58:47.220Z","__v":0,"aromaTags":{"Intensity":{"values":["Powerful"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Peel/Skin","Ripe","Fresh"]},"Non-Fruit":{"values":["Herbal","Spice","Wood"]},"Organic/Inorganic":{"values":["Stone"]},"Oak Intensity":{"values":["Low"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderate"]},"Acid":{"values":["Moderate"]},"Alcohol":{"values":["Moderately Low"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Peel/Skin","Tart","Fresh"]},"Non-Fruit":{"values":["Herbal","Spice","Licorice","Wood"]},"Organic/Inorganic":{"values":["Stone"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Few"]},"Color":{"values":["Ruby"]},"Color Intensity":{"values":["Medium"]}},"uWineData":{"_id":"5dce97cfe78d867664213236","scoreCount":1,"averageScore":95.1,"userWines":["5dbe0233e78d86766421317b","5dfea3bde78d8676642135f4"],"expertScore2":"","expertReviewer2":"none","expertScore1":"","expertReviewer1":"none","currency":"USD","commonPrice":14,"additionalDetails":"","designation":"Firehouse Red","category":"Red","varietal":"Red Blend","vineyard":"","appellation":"Columbia Valley","subRegion":"Columbia Valley","region":"Washington","country":"United States","wineryUrl":"http://www.tamarackcellars.com","winery":"Tamarack cellars","vintage":"2016","deleted":false,"lastUpdated":"2019-12-21T23:15:06.578Z","created":"2019-11-15T12:19:27.348Z","__v":2}},{"_id":"5df533bee78d8676642134d4","photoURL":"077d87cb7aac91cc59f3a0cf1f981197","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dfb322fe78d867664213582","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"Moderately big wine. Heavy acidity, tartness, and with a great red and blue fruit aromas.","scoreTotal":90.9,"scoreOverallImpression":4.1,"scoreFinish":3.9,"scoreTaste":4.1,"scoreAroma":4.3,"lastUpdated":"2020-05-14T22:31:13.806Z","created":"2020-05-14T22:31:13.807Z","__v":0,"aromaTags":{"Intensity":{"values":["Moderate"]},"Fruit":{"values":["Red Fruit","Blue Fruit"]},"Fruit Character":{"values":["Ripe","Jammy"]},"Non-Fruit":{"values":["Floral","Herbal","Spice"]},"Organic/Inorganic":{"values":["Stone"]},"Oak Intensity":{"values":["None"]}},"finishTags":{"Length/Finish":{"values":["Medium Long"]}},"overallTags":{"Is the Wine Balanced?":{"values":["No"]},"Balance: If No, Which of the Following Elements Stick Out?":{"values":["Acidity","Tannin"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Dry"]},"Tannins":{"values":["Moderately High"]},"Acid":{"values":["Moderately High"]},"Alcohol":{"values":["Moderately Low"]},"Body/Texture":{"values":["Full"]},"Fruit":{"values":["Citrus","Red Fruit","Blue Fruit"]},"Fruit Character":{"values":["Tart","Ripe","Fresh"]},"Non-Fruit":{"values":["Herbal","Wood"]},"Organic/Inorganic":{"values":["Stone","Petrol"]}},"visualTags":{"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Few"]},"Color":{"values":["Ruby"]},"Color Intensity":{"values":["Deep"]}},"uWineData":{"_id":"5dfb322fe78d867664213582","scoreCount":1,"averageScore":0,"userWines":["5df533bee78d8676642134d4"],"expertScore2":"","expertReviewer2":null,"expertScore1":"","expertReviewer1":null,"currency":"USD","commonPrice":30,"additionalDetails":"","designation":"","category":"Red","varietal":"Red Bordeaux Blend","vineyard":"","appellation":"Saint-Emilion Grand Cru","subRegion":"Saint-Emilion","region":"Bordeaux","country":"France","wineryUrl":"","winery":"Chateau Patris Querre","vintage":"2016","deleted":false,"lastUpdated":"2019-12-19T08:18:00.075Z","created":"2019-12-19T08:17:51.237Z","__v":1}},{"_id":"5df53385e78d8676642134d3","photoURL":"a7beda1ac343ff8d7fffccd32208db84","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dfb3795e78d86766421358d","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"Earthy cherry with a light ashy finish.","scoreTotal":91.5,"scoreOverallImpression":4.2,"scoreFinish":4,"scoreTaste":4.2,"scoreAroma":4.2,"lastUpdated":"2020-05-06T00:01:34.559Z","created":"2020-05-05T23:20:15.043Z","__v":0,"aromaTags":{"Intensity":{"values":["Moderate"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Tart","Ripe","Fresh"]},"Non-Fruit":{"values":["Herbal"]},"Oak Intensity":{"values":["None"]}},"finishTags":{"Length/Finish":{"values":["Medium"]}},"overallTags":{"Is the Wine Balanced?":{"values":["Yes"]},"Complexity":{"values":["Moderate"]}},"tasteTags":{"Sweetness":{"values":["Off Dry"]},"Tannins":{"values":["Moderately Low"]},"Acid":{"values":["Moderate"]},"Alcohol":{"values":["Moderate"]},"Body/Texture":{"values":["Medium"]},"Fruit":{"values":["Red Fruit"]},"Fruit Character":{"values":["Tart","Ripe","Fresh"]},"Non-Fruit":{"values":["Spice","Licorice"]}},"visualTags":{"Clarity":{"values":["Clear"]},"Brightness":{"values":["Bright"]},"Sediment or Particles":{"values":["No"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Lot"]},"Color":{"values":["Ruby"]},"Color Intensity":{"values":["Pale"]}},"uWineData":{"_id":"5dfb3795e78d86766421358d","scoreCount":1,"averageScore":0,"userWines":["5df53385e78d8676642134d3"],"expertScore2":"","expertReviewer2":null,"expertScore1":"90","expertReviewer1":"WE","currency":"USD","commonPrice":39,"additionalDetails":"","designation":"Barrel Select","category":"Red","varietal":"Pinot Noir","vineyard":"","appellation":"Willamette Valley","subRegion":"Willamette","region":"Oregon","country":"United States","wineryUrl":"https://www.wvv.com","winery":"Willamette Valley Vineyards","vintage":"2017","deleted":false,"lastUpdated":"2019-12-19T08:41:08.067Z","created":"2019-12-19T08:40:53.913Z","__v":1}},{"_id":"5df533d2e78d8676642134d5","photoURL":"4d6d4c42bc5218d64fb134f4a8eb9f5f","user":"554f99352ee62248071b4d0f","mode":"past","wineUniversal":"5dfb3158e78d86766421357f","hidden":false,"deleted":false,"eventBlindTasting":false,"quantity":1,"groupDescription":"none","comment":"","scoreTotal":89.3,"scoreOverallImpression":3.9,"scoreFinish":3.8,"scoreTaste":4,"scoreAroma":4,"lastUpdated":"2020-03-18T23:09:30.869Z","created":"2020-03-18T23:09:30.869Z","__v":0,"aromaTags":{"Oak Intensity":{"values":["None"]},"Non-Fruit":{"values":["Floral","Herbal"]},"Fruit Character":{"values":["Peel/Skin","Tart","Fresh"]},"Fruit":{"values":["Citrus","Red Fruit"]}},"finishTags":{"Length/Finish":{"values":["Medium"]}},"overallTags":{"Complexity":{"values":["Moderate"]},"Balance: If No, Which of the Following Elements Stick Out?":{"values":["Acidity"]},"Is the Wine Balanced?":{"values":["No"]}},"tasteTags":{"Non-Fruit":{"values":["Herbal","Spice","Wood"]},"Fruit Character":{"values":["Peel/Skin","Tart","Fresh"]},"Fruit":{"values":["Red Fruit"]},"Body/Texture":{"values":["Medium"]},"Alcohol":{"values":["Moderate"]},"Acid":{"values":["Moderately High"]},"Tannins":{"values":["Moderately Low"]},"Sweetness":{"values":["Dry"]}},"visualTags":{"Color Intensity":{"values":["Medium"]},"Color":{"values":["Ruby"]},"Legs on the side of the glass (Viscosity/Tears)":{"values":["A Lot"]},"Sediment or Particles":{"values":["No"]},"Brightness":{"values":["Bright"]},"Clarity":{"values":["Clear"]}},"uWineData":{"_id":"5dfb3158e78d86766421357f","scoreCount":1,"averageScore":0,"userWines":["5df533d2e78d8676642134d5"],"expertScore2":"","expertReviewer2":null,"expertScore1":"","expertReviewer1":null,"currency":"USD","commonPrice":42,"additionalDetails":"","designation":"Stanley Groovy","category":"Red","varietal":"Red Blend","vineyard":"","appellation":"Red Mountain","subRegion":"Columbia Valley","region":"Washington","country":"United States","wineryUrl":"https://www.thewallsvineyards.com","winery":"The Walls Vineyards","vintage":"2017","deleted":false,"lastUpdated":"2019-12-19T08:14:22.142Z","created":"2019-12-19T08:14:16.093Z","__v":1}}]

这是一个没有$unwind的文档:

{
"_id": "5ed5b3d186bff9019b43d173",
"scoreAroma": 4.5,
"scoreTaste": 4.5,
"scoreFinish": 4.3,
"scoreOverallImpression": 4.4,
"scoreTotal": 94.3,
"comment": "Amazingly delicious!",
"groupDescription": "none",
"quantity": 1,
"eventBlindTasting": false,
"deleted": false,
"hidden": false,
"wineUniversal": null,
"created": "2020-06-05T03:18:09.428Z",
"mode": "past",
"lastUpdated": "2020-06-05T03:18:09.428Z",
"user": "554f99352ee62248071b4d0f",
"photoURL": "00d28cb99a8460816e0c079cea8f9f45",
"uWineData": []
}

这是一个添加了$unwind步骤的文档:

{
"_id": "5dbbc408e78d867664213147",
"photoURL": "51972ee99dec8f31cdb6ff8025a0d3d3",
"user": "554f99352ee62248071b4d0f",
"mode": "past",
"wineUniversal": "5dce6038e78d8676642131fd",
"hidden": false,
"deleted": false,
"eventBlindTasting": false,
"quantity": 1,
"groupDescription": "none",
"comment": "Surprisingly tasty. You'd think it's be olonk based on the gimmicky label.",
"scoreTotal": 91,
"scoreOverallImpression": 4.2,
"scoreFinish": 4,
"scoreTaste": 4,
"scoreAroma": 4.3,
"lastUpdated": "2020-05-21T23:06:54.497Z",
"created": "2020-05-21T23:06:54.498Z",
"uWineData": {
"_id": "5dce6038e78d8676642131fd",
"scoreCount": 1,
"averageScore": 0,
"userWines": ["5dbbc408e78d867664213147"],
"expertScore2": "",
"expertReviewer2": null,
"expertScore1": "",
"expertReviewer1": null,
"currency": "USD",
"commonPrice": 12,
"additionalDetails": "",
"designation": "",
"category": "Red",
"varietal": "Cabernet Sauvignon",
"vineyard": "",
"appellation": "California",
"subRegion": "",
"region": "California",
"country": "United States",
"wineryUrl": "https://www.thewalkingdeadwine.com",
"winery": "The Walking Dead Wines",
"vintage": "2016",
"deleted": false,
"lastUpdated": "2019-11-15T08:22:32.437Z",
"created": "2019-11-15T08:22:16.579Z",
"__v": 1
}
}

如果我在管道中没有$unwind步骤的情况下运行聚合调用,则排序顺序是正确的。但是,一旦我添加$unwind步骤,我的排序顺序就会给我不同的排序结果。这是为什么呢?

可能的问题: 问题是否与调用$unwind步骤后文档中有两个created属性有关?如果这导致了问题,我如何定位顶级created

事实证明,对于最新的顶级文档,还没有链接到它的子文档。我将preserveNullAndEmptyArrays: true属性添加到我的$unwind步骤中,现在排序的行为符合预期。

{
"path": "$uWineData",
"preserveNullAndEmptyArrays": true
}

最新更新