[SERVER-43030] sort before group, without changing key... Created: 25/Aug/19  Updated: 26/Aug/19  Resolved: 26/Aug/19

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

Type: Bug Priority: Major - P3
Reporter: Hassan Faghihi Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2016
MongoDB 4.0.4


Issue Links:
Duplicate
duplicates SERVER-43029 sort before group, without changing k... Closed
Operating System: ALL
Participants:

 Description   

I was working with mongo db on my project for quite some time using test pre-generated data, until i insert this record, and i found something strange, the data returned in middle of list...?
shouldn't it be sorted? shouldn't it comes last or first? in inserted order... after some search i found that the data my return in any order and i need to sort it...

so i did the sorting at one of beginning stage, before $lookup, but still it was wrong, i move it to last, and it worked, so i push sort down stage by stage, until i caught it causing trouble at one of the middle stages, right before $group stage...
but the issue is not grouping, it is that i group by _id... fixing one of my issue that you may had another answer for it, but i did it this way... and now i found $group guilty.

In trivial way, the _id is one (I mean there is no duplication version), and it scan in order, shouldn't it result in order too?

Generated by Studio 3T

 

// Requires official MongoShell 3.6+
use realEstate;
db.getCollection("basePlace").aggregate(
    [
        { 
            "$lookup" : {
                "from" : "humanResource.user", 
                "localField" : "UserId", 
                "foreignField" : "_id", 
                "as" : "User"
            }
        }, 
        { 
            "$unwind" : {
                "path" : "$User", 
                "preserveNullAndEmptyArrays" : true
            }
        }, 
        { 
            "$addFields" : {
                "User.ProfilePictures" : {
                    "$map" : {
                        "input" : "$User.ProfilePictures", 
                        "as" : "pp", 
                        "in" : {
                            "ImageId" : "$$pp.ImageId", 
                            "CloudinaryUrl" : "$$pp.CloudinarySecureUrl", 
                            "IsMain" : "$$pp.IsMain", 
                            "Title" : "$$pp.Title", 
                            "Description" : "$pp.Description"
                        }
                    }
                }, 
                "Images" : {
                    "$map" : {
                        "input" : "$Images", 
                        "as" : "pp", 
                        "in" : {
                            "ImageId" : "$$pp.ImageId", 
                            "CloudinaryUrl" : "$$pp.CloudinarySecureUrl", 
                            "IsMain" : "$$pp.IsMain", 
                            "Title" : "$$pp.Title", 
                            "Description" : "$pp.Description"
                        }
                    }
                }, 
                "Maps" : {
                    "$map" : {
                        "input" : "$Maps", 
                        "as" : "pp", 
                        "in" : {
                            "ImageId" : "$$pp.ImageId", 
                            "CloudinaryUrl" : "$$pp.CloudinarySecureUrl", 
                            "IsMain" : "$$pp.IsMain", 
                            "Title" : "$$pp.Title", 
                            "Description" : "$pp.Description"
                        }
                    }
                }
            }
        }, 
        { 
            "$addFields" : {
                "User.ProfilePictures" : {
                    "$cond" : [
                        {
                            "$and" : [
                                {
                                    "$ne" : [
                                        "User.ProfilePictures", 
                                        undefined
                                    ]
                                }, 
                                {
                                    "$ne" : [
                                        "User.ProfilePictures", 
                                        null
                                    ]
                                }
                            ]
                        }, 
                        {
                            "$cond" : [
                                {
                                    "$ne" : [
                                        {
                                            "$indexOfArray" : [
                                                "$User.ProfilePictures.IsMain", 
                                                true
                                            ]
                                        }, 
                                        -1.0
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$User.ProfilePictures", 
                                        {
                                            "$indexOfArray" : [
                                                "$User.ProfilePictures.IsMain", 
                                                true
                                            ]
                                        }
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$User.ProfilePictures", 
                                        0.0
                                    ]
                                }
                            ]
                        }, 
                        null
                    ]
                }, 
                "Images" : {
                    "$cond" : [
                        {
                            "$and" : [
                                {
                                    "$ne" : [
                                        "Images", 
                                        undefined
                                    ]
                                }, 
                                {
                                    "$ne" : [
                                        "Images", 
                                        null
                                    ]
                                }
                            ]
                        }, 
                        {
                            "$cond" : [
                                {
                                    "$ne" : [
                                        {
                                            "$indexOfArray" : [
                                                "$Images.IsMain", 
                                                true
                                            ]
                                        }, 
                                        -1.0
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$Images", 
                                        {
                                            "$indexOfArray" : [
                                                "$Images.IsMain", 
                                                true
                                            ]
                                        }
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$Images", 
                                        0.0
                                    ]
                                }
                            ]
                        }, 
                        null
                    ]
                }, 
                "Maps" : {
                    "$cond" : [
                        {
                            "$and" : [
                                {
                                    "$ne" : [
                                        "Maps", 
                                        undefined
                                    ]
                                }, 
                                {
                                    "$ne" : [
                                        "Maps", 
                                        null
                                    ]
                                }
                            ]
                        }, 
                        {
                            "$cond" : [
                                {
                                    "$ne" : [
                                        {
                                            "$indexOfArray" : [
                                                "$Maps.IsMain", 
                                                true
                                            ]
                                        }, 
                                        -1.0
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$Maps", 
                                        {
                                            "$indexOfArray" : [
                                                "$Maps.IsMain", 
                                                true
                                            ]
                                        }
                                    ]
                                }, 
                                {
                                    "$arrayElemAt" : [
                                        "$Maps", 
                                        0.0
                                    ]
                                }
                            ]
                        }, 
                        null
                    ]
                }
            }
        }, 
        { 
            "$sort" : {
                "_id" : -1.0
            }
        }, 
        { 
            "$group" : {
                "_id" : "$_id", 
                "Images" : {
                    "$push" : "$Images"
                }, 
                "Maps" : {
                    "$push" : "$Maps"
                }, 
                "allData" : {
                    "$first" : "$$ROOT"
                }
            }
        }, 
        { 
            "$addFields" : {
                "allData.Images" : "$Images", 
                "allData.Maps" : "$Maps"
            }
        }, 
        { 
            "$replaceRoot" : {
                "newRoot" : "$allData"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

 


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