[SERVER-43855] Aggregation results aren't the same. Created: 05/Oct/19  Updated: 27/Oct/23  Resolved: 07/Oct/19

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 4.2.0
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Sergey Protsak Assignee: Carl Champain (Inactive)
Resolution: Community Answered Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive aggregate.zip    
Participants:

 Description   

In the attachment you can find two collections and aggregation.

If you repeat aggregation many times, result will be different.

Why? It must be the same.



 Comments   
Comment by Asya Kamsky [ 07/Oct/19 ]

serpro.qnx@gmail.com

There are two issues happening in your pipeline that are causing you to see slightly incorrect results. One is that you have some game_id values which don't have corresponding values in the games collection, this results in "item.name" being missing and while normally it would cause an error to ask $arrayToObject to convert a missing key name the reason you're not seeing the error in your pipeline is due to the way you are generating the array of two values using $zip. Since you are passing "$items.name" and "$items.total" to $zip, what's happening is that your "name" array is one shorter than your "total" array, causing the last "total" to be dropped from the final object. Since we don't guarantee an order of fields (because you didn't specify that "$items.name" should be sorted) it's likely that what you are seeing is different single value being dropped in the final result document each time you run this pipeline.

I would recommend that you change this expression:

name: '$name' 

to utilize $ifNull expression to set the name to some fixed value (like 'NONAME') or maybe filter out the documents which don't find a matching game in the lookup table if that's more appropriate to your case.

Note that to guarantee that the fields are exactly the same including order in the final output, you should change

{$sort: {  "items.total": -1 }} 

to be

 {$sort: {  "items.total": -1, "items.name":1 }}  


I also noticed that you have some inefficiencies (unnecessary stages) in the pipeline, if you'd like further assistance with it, I encourage you to ask our community by posting on the mongodb-user group.

Since there is no bug here, I'm going to close this ticket now.
Thank you for using MongoDB.

Generated at Thu Feb 08 05:04:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.