Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$function combines "return" and next word/characters when execute the query #22

Open
turivishal opened this issue Aug 20, 2020 · 4 comments

Comments

@turivishal
Copy link

turivishal commented Aug 20, 2020

Reference: $function operator from MongoDB v4.4,

Original Code (Working in Mongo Shell):

db.collection.aggregate([
  {
    $addFields: {
      locations: {
        $function: {
          body: function(locations, sort_category){
            return locations.sort(function(a, b){
              return a[sort_category]-b[sort_category]
            })
          },
          args: ["$locations", "$sort_category"],
          lang: "js"
        }
      }
    }
  }
])

Implementation on Playground:
https://mongoplayground.net/p/Jq4JdgDf3K2

Behavior after executing in MongoPlayground :
It will combines return with next word,

db.collection.aggregate([
  {
    $addFields: {
      locations: {
        $function: {
          body: function(locations, sort_category){

            /* below line should be "return locations.sort(function(a, b){" */
            returnlocations.sort(function(a, b){

              /* below line should be "return a[sort_category]-b[sort_category]" */
              returna[sort_category]-b[sort_category]

            })
          },
          args: ["$locations", "$sort_category"],
          lang: "js"
        }
      }
    }
  }
])
@feliixx
Copy link
Owner

feliixx commented Aug 31, 2020

Hi @turivishal

thanks for the detailed report ! This is a bug from the formatter, it doesn't works well with javascript right now :/ I'll try to fix this next week

@turivishal
Copy link
Author

@feliixx we are really waiting for this operator to be support in playground.

@feliixx
Copy link
Owner

feliixx commented Jan 13, 2021

Hi @turivishal,

I'm planning to fix this soon, in the meantime I've found that enclosing the whole function in double quote works, but it's ugly as it has to be on a single line:

db.collection.aggregate([
  {
    $addFields: {
      locations: {
        $function: {
          body: "function(locations, sort_category){ return locations.sort(function(a, b){ return a[sort_category]-b[sort_category]})}",
          args: [
            "$locations",
            "$sort_category"
          ],
          lang: "js"
        }
      }
    }
  }
])

see: https://mongoplayground.net/p/CEecvX7XLuH

@turivishal
Copy link
Author

@feliixx it will work for now, Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants