在mongoDB的system.js中存储库的技术 [英] techniques for storing libraries in mongoDB's system.js

查看:110
本文介绍了在mongoDB的system.js中存储库的技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mongoDB的 dateJS 格式时遇到了这个问题.JIRA #SERVER-770解释说,对象的闭包(包括其原型)在序列化到system.js集合时会丢失,这是预期的行为.不幸的是,这排除了许多很棒的框架,例如 dojo jQuery .

Are there any reliable techniques for storing prototype-based libraries/frameworks in mongoDB's system.js? I came across this issue when trying to use dateJS formats within a map-reduce. JIRA #SERVER-770 explains that objects' closures - including their prototypes - are lost when serialized to the system.js collection, and that this is the expected behavior. Unfortunately, this excludes a lot of great frameworks such as dojo, Google Closure, and jQuery.

是否有某种方式可以转换或包含不依赖原型的库?有一些希望在Map-Reduce之前进行初始化并将它们传递给scope对象,但是到目前为止我还没有什么运气.如果我的方法有缺陷,什么是使mongo可以重新使用服务器端javascript的更好方法?

Is there a way to somehow convert or contain libraries such that they don't rely on prototyping? There's some promise to initializing before the Map-Reduce and passing them in through the scope object, but I haven't had much luck so far. If my approach is flawed, what is a better way to enable server-side javascript re-use for mongo?

推荐答案

使用JS的每个查询都可以重用或获取全新的JS上下文,并在该上下文上加载存储的JS对象.为了做您想做的事,您需要:

Every query using JS may reuse or get a brand new JS context, on which stored JS objects are loaded. In order to do what you want, you need either:

  1. mongod在安装时自动运行存储的代码
  2. mapreduce有一个init方法

第一个绝对是更有趣的功能.事实证明,mongodb v8构建会自动执行此操作(但未得到官方支持),但不是官方的spidermonkey构建.

The first is definitely the more interesting feature. Turns out that mongodb v8 build automatically does it (but not officially supported), but not the official spidermonkey build.

假设您存储如下代码:

db.system.js.save({ _id: "mylib", value: "myprint = function() { print('installed'); return 'installed';" }

然后在v8中,您可以在代码中自由使用myprint(),但是对于SM,则需要显式调用mylib().

Then in v8 you can use myprint() freely in your code, but with SM you would need to call mylib() explicitly.

作为解决方法,您可以创建另一个方法:

As a workaround you can create another method:

db.system.js.save({ _id: "installLib", value: "if (!libLoaded) mylib(); libLoaded = true;" }

然后从您的map()函数调用它.

And call it from your map() function.

创建票证以使引擎标准化并允许自动运行: https://jira.mongodb.org/browse/SERVER-4450

Created ticket in order to standardize engines and allow automatic run: https://jira.mongodb.org/browse/SERVER-4450

这篇关于在mongoDB的system.js中存储库的技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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