in Robo3T, how to add a custom function

Viewed 7840

I just installed Robo3T( previously named robomongo), which has a GUI for mongo db. I want to add a custom function to help me get the last N documents in normal order, the query statement is like this:

db.getCollection('i1801').find().skip(db.getCollection('i1801').count() - 1200)

And I found from stackoverflow(mongodb: how to get the last N records?), this can be written as a function like this:

function last(N) {
    return db.collection.find().skip(db.collection.count() - N);
}

And I go back to my Robo3T, trying to add a custom function last(), but didn't work, nothing showed up under function tab.

I have attached some screen captures illustrating this issue:

Trying to add a custom function

Created a custom function and saved

After save, nothing happened

After I clicked save button, nothing happened, still no functions under function tab. And the log shows function last created, and function tab being refreshed. Here is the logs

So, how can I add a last function here?

2 Answers

Tried the steps you listed, it worked for simple find statements. When I added the 'skip', the save failed. Even updating an existing function didn't work. Maybe an issue with Robo? I dont know.

Anyway, to add the function in Robo3t

  1. Right click collection
  2. Select Open Shell
  3. On the shell type the following

db.system.js.save(
   {
     _id: "last",
     value : function last(x) { return db.test.find().skip(db.test.count() - x); }
   }
)

Lastly, Run the script and refresh Function folder.

Related