Using $function to run a javascript function is not working in mongosh

Hi all. I’m trying to use a function in order to parse a json format field in a collection. But it seems that I’m missing some previous steps in order to do so, if I run a simple aggregation like this:

db.movies.aggregate([
    { 
      $addFields: {
        fromFunction: {
          $function: {
            body: "function(){return 'hello'}",
            args: [], 
            lang: 'js'
          }
        }
       }
     }
  ])

nothing happens, the collection is not created ( Reference: How to Use Custom Aggregation Expressions in MongoDB 4.4 | MongoDB).
Do I need to install any driver or package, or setup the shell in order to run such javascript function? I’m using MongoDB Compass Shell. Versions: MongoDB Compass 1.36.4, mongoDB 6.0.6

Hi @Luis_Leon - Welcome to the community :wave:

I presume you’re expecting it to return a document with an additional field called fromFunction with the value 'hello' - please correct me if I’m wrong here.

Can you try running the aggregation below?:

db.movies.aggregate([
  {
    '$addFields': {
      fromFunction: { '$function': { body: function(){return 'hello'}, args: [], lang: 'js' } }
    }
  }
])

If nothing is returned, can you advise the output of db.movies.countDocuments()?

Regards,
Jason

Hello Jason, thanks for your reply. I tried running your aggregation, but it justs hangs and the prompt is not returned. By the way, when I tried running a function using JSON.parse it behave the same way, it just hanged, that’s why I tried running a simpler function in order to see if the shell configuration was Ok.

Hi Luis,

Looks like theres no documents in that collection. Can you try the following on a test environmentand let me know the output?

  1. use test - Switch to "test" database
  2. db.movies.insertOne({a:1}) - Insert a sample / test document into the "movies" collection
  3. Run:
db.movies.aggregate([
  {
    '$addFields': {
      fromFunction: { '$function': { body: function(){return 'hello'}, args: [], lang: 'js' } }
    }
  }
])

Additionally, when you state the prompt hangs, do you mean that the test> indicator in the embedded mongoshell in compass is not returned and you cannot type further?

Look forward to hearing from you.

Regards,
Jason

Hi Json. Step 2 runs ok

Step 3, again the prompt hangs, with that I mean that the test indicator in the embedded mongoshell never returns, the only way to quit is closing Compass window and start again, like picture below:

Am I missing some driver or software installation in order to run a javascript function?

1 Like

What version of Compass are you using? Is this behaviour replicated if you try with the standalone mongosh shell?

I’ll try replicate this behaviour using Compass on my own test environment.

Regards,
Jason

Tested via mongosh on my test environment, the output is per the below code snippets:

test> db.movies.insertOne({a:1})
{
  acknowledged: true,
  insertedId: ObjectId("646ff4aa0929e08e64012e21")
}
test> db.movies.find()
[ { _id: ObjectId("646ff4aa0929e08e64012e21"), a: 1 } ]

The aggregation with $function:

test> db.movies.aggregate([
  {
    '$addFields': {
      fromFunction: { '$function': { body: function(){return 'hello'}, args: [], lang: 'js' } }
    }
  }
])
[
  {
    _id: ObjectId("646ff4aa0929e08e64012e21"),
    a: 1,
    fromFunction: 'hello'
  }
]

I was able to replicate the prompt hanging in the embedded mongosh shell in Compass. It does work from the standalone mongosh so if you’re able to try that in the mean time hopefully it works for you.

Yes, it seems to work in standalone shell and something is wrong in Compass. Even trying to visualize the document in compaas seems to be wrong.

Good news is javascript function is running ok now using standalone shell. Now the problem is different, but I think I will open a new topic. Thanks Jason

1 Like

Thanks for updating the post with confirmation Luis :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.