简体   繁体   English

在 Robo3T 中,如何添加自定义功能

[英]in Robo3T, how to add a custom function

I just installed Robo3T( previously named robomongo), which has a GUI for mongo db.我刚刚安装了 Robo3T(以前命名为 robomongo),它有一个用于 mongo db 的 GUI。 I want to add a custom function to help me get the last N documents in normal order, the query statement is like this:我想添加一个自定义函数,帮助我按正常顺序获取最后N个文档,查询语句是这样的:

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:我从 stackoverflow( mongodb: how to get the last N records? ) 中发现,这可以写成这样的函数:

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.然后我回到我的 Robo3T,尝试添加一个自定义函数last() ,但没有用,函数选项卡下没有显示任何内容。

I have attached some screen captures illustrating this issue:我附上了一些说明此问题的屏幕截图:

尝试添加自定义函数

创建自定义函数并保存

保存后什么都没发生

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.并且日志显示last创建的功能,以及正在刷新的功能选项卡。 这是日志

So, how can I add a last function here?那么,如何在此处添加last函数?

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?也许是 Robo 的问题? I dont know.我不知道。

Anyway, to add the function in Robo3t无论如何,在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.最后,运行脚本并刷新 Function 文件夹。

For calling function:对于调用函数:

db.loadServerScripts(); db.loadServerScripts();
callyourfunction();调用你的函数();

  1. Right click on collection右键点击收藏
  2. Click on open shell点击打开外壳
    https://i.stack.imgur.com/YTiGc.jpg https://i.stack.imgur.com/YTiGc.jpg
  3. In shell run db.loadServerScripts();在 shell 中运行 db.loadServerScripts();
    https://i.stack.imgur.com/BDTBT.jpg https://i.stack.imgur.com/BDTBT.jpg
  4. call user function like: addTwo(2,5);调用用户函数,如: addTwo(2,5);
    https://i.stack.imgur.com/EujGo.jpg https://i.stack.imgur.com/EujGo.jpg

NOTE :: For seen output, Make sure your function should be a return value注意 :: 对于可见的输出,请确保您的函数应该是返回值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM