Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-43029

sort before group, without changing key...

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.4
    • Component/s: Aggregation Framework
    • Labels:
    • Environment:
      Windows Server 2016
      MongoDB 4.0.4
    • Fully Compatible
    • ALL

      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
          }
      );
      
      

       

            Assignee:
            carl.champain@mongodb.com Carl Champain (Inactive)
            Reporter:
            deadmann Hassan Faghihi
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: