mongodb服务器端javascript实际上是客户端? [英] mongodb server-side javascript is client-side actually?

查看:106
本文介绍了mongodb服务器端javascript实际上是客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量文档,我想提取一些统计数据。它需要每15分钟定期执行一次。

I have a huge collection of documents and I want to extract some statistics on it. It needs to be executed periodically each 15 minutes.

大多数统计数据都是基于文档大小的,因此我需要获取文档并计算其大小。

Most of the stats are based on the document size, so I need to fetch the documents and calculate its size.

我的统计数据的输出只是一行,有一些关于文件大小的统计数据。 (我不是整个集合,只是它的一部分,所以我不能使用mongodb提供的集合统计数据)

The output of my stats is just a single line with some stats, regarding the size of the documents. (I am not fetching a whole collection, just a subset of it, so I cannot use the collection stats provided by mongodb)

我想要的是制作这个在服务器端执行,并避免将所有文档传输到客户端,(只是因为我需要计算大小)。

What I'd like is to make this execution on the server side, and avoid transferring all the documents to the client side, (just because I need to calculate the size).

我用mongo shell执行它,确保我连接到辅助设备,并且此 mongo shell始终在远程计算机上运行,因此这是避免通过网络传输所有文档的主要原因。

I am executing it with mongo shell, making sure I am connecting to a secondary, and this mongo shell is always running in a remote machine, so this is the main reason to avoid transferring all documents through the network.

在阅读mongo shell文档之后,我预计它将在服务器端执行,因为它说明了,但它不是以这种方式工作而是在与mongo shell相同的机器上执行(在我看来,这比服务器端更客户端。)

After reading the mongo shell documentation I expected it to be executed "server-side" as it states, but it is not working this way and it is being executed in the same machine as the mongo shell (which is more client-side than server-side in my opinion).

我正在粘贴代码的摘录以防万一它有帮助:

I am pasting an extract of my code just in case it helps :

db.cache.find(query).forEach(function(obj) {

        var curr = Object.bsonsize(obj); 

        if(stats.max < curr) {
            stats.max = curr;
            stats.maxid = obj._id;
        } 
        if(stats.min > curr) {
            stats.min = curr;
        } 
        stats.count++;
        stats.total += curr;

        stats.avg = stats.total/stats.count;
    })

如果我在本地执行mongo shell需要3-4秒在远程执行的mongo shell中超过1分钟。

It takes like 3-4 seconds if I execute mongo shell locally and more than 1 minute in a mongo shell executed remotely.

如何使这个服务器端javascript成为真正的服务器端执行?

Any ideas how to make this server side javascript be a real server side execution?

更新:

总结答案中提到的选项:

To summarize the options mentioned in the answer :


  • 使用 system.js 集合+ db.eval :我不能使用它,因为 eval 已弃用,但 eval 需要在主服务器上运行,我必须运行它是在次要的。

  • use system.js collection + db.eval : I cannot use it because eval is deprecated, but also eval needs to run on the master, and I have to run it on a secondary.

使用 system.js 集合+ loadServerScripts :它执行mongo shell机器中的javascript代码,即客户端。

use system.js collection + loadServerScripts : It executes the javascript code in the mongo shell machine, which is the "client".

cron job:我需要要在特定节点上运行它,并且由于master可能会更改为另一个节点,我最终可能会对主节点运行它,我应该避免。但是,我不允许这样做,其中一个要求是在远程shell上运行它。 (有几个像这样的dbs需要这种统计数据,并且更容易只在一个地方使用它)。

cron job : I'd need to run it on a specific node, and as master may change to another node, I can end up running it against the master which I should avoid. But also, I am not allowed to do so, one of the requirements is to run it on a remote shell. (There are several dbs like these one that will need this kind of statistics, and it is easier to mantain having it only in one place).

推荐答案

您可以将js代码存储为一种存储程序

You could store js code as a kind of stored procedure.

根据本文,您可以将js存储为系统调用:

As per this article you can store js as a system call:

 db.system.js.save({_id: "sum", value: function (x, y) { return x + y; }});

然后称之为:

db.eval("return sum(2, 3);");




因为eval被折旧 - 没有时间设置它将是已禁用请参阅此处



db.loadServerScripts();
sum(3,2) 

额外文件这里

这篇关于mongodb服务器端javascript实际上是客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆