如何通过Mongoose使用mongo db.eval [英] how to use mongo db.eval through Mongoose

查看:195
本文介绍了如何通过Mongoose使用mongo db.eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mongo shell中,我可以使用db.eval运行函数,例如:

From my mongo shell I can run functions with db.eval like:

db.eval('return 7;');

并且,但是对于弃用警告,参数功能可以正常运行.

And, but for a deprecation warning, the parameter function is run properly.

现在我想通过mongoosenode.js调用这样的脚本".

Now I want to call such a 'script' from node.js, through mongoose.

我尝试仅使用字符串化的功能代码作为参数调用db.eval(如

I've tried to just call db.eval with the stringified function code as parameter (as in here):

mongoose.connect(PATH_TO_A_MONGO_DATABASE);
mongoose.connection.db.eval('return 9;', function(err, result){
    console.log(result);
    //etc.
}); 

这默默地忽略了回调.不会引发任何错误,并且永远不会调用回调函数.

This silently ignores the callback. No error is thrown, and the callback function is never called.

这样做时,我检查了mongoose.connection.db.eval实际上是一个函数.

When doing this, I checked that mongoose.connection.db.eval is actually a function.

由于db.eval也具有Promise接口,因此也尝试过:

Since db.eval also has a Promise interface, also tried:

mongoose.connection.db.eval('return 5;').then(console.log).catch(console.log);

结果相同:没有错误,只有沉默.

With the same result: no errors, just silence.

因此,我想我的语法缺少一些内容,但是我找不到有关此内容的文档或示例. 类似的任何问题

So, I guess I'm missing something with the syntax, but I can't find documentation or examples about this. Any similar questions like this or this didn't help.

PD1:我愿意这样做,因为我需要通过mongoose调用存储在system.js中的过程.我也做不到但是,我什至无法运行像return 5;这样的愚蠢脚本,因此我之前要求的是更简单的任务.欢迎提供有关如何使用猫鼬调用服务器脚本的任何建议.

PD1: I'm willing to do this because I need to call a procedure stored in system.js through mongoose. I can't do that too. However, I can't even run a silly script like return 5;, so I'm asking for the simpler task before. Any advice on how to call server scripts with mongoose is welcome.

PD2:我知道在mongo中存储服务器脚本是一种不好的做法,并且由于某种原因而弃用了它们,依此类推……但是我不能就此做出决定.我被告知要在我的公司这样做,而现在不在这里设置存储的服务器脚本的原始代码的同事.

PD2: I'm aware stored server scripts in mongo are a bad practice, and that they are deprecated for a reason and so on... but I can't just decide about that. I've been told to do that at my company, and the co-worker who set up the original code of the stored server script is not here now.

推荐答案

好,我知道了为什么我的db.eval回调被忽略的原因.它与猫鼬连接有关.

Ok, I figured out why my db.eval callbacks was being ignored. It is related with the mongoose connection.

如果您启动像这样的连接:

If you start a connection like:

const conn = mongoose.connect(PATH_TO_DB);

然后进行查询:

conn.model(a_collection, a_schema).find({}).exec().then()...

即使您只是在连接后立即调用查询(从逻辑上讲,它是一个异步过程),猫鼬也认为它必须等待连接处于正确的状态,然后触发查询,然后调用回调查询结果.

Even if you just call the query right after the connection -it is, logically, an asynchronous process-, mongooose figures that it has to wait to the connection to be in a proper state, then fire the query, then call the callback with the query results.

但是,这与db.eval()的工作方式不同.只是尝试在调用连接之后立即调用db.eval(),该回调会被静默忽略:

However, this doesn't work the same way with db.eval(). just trying to call db.eval() right after the call to the connection, the callback is silently ignored:

const conn = mongoose.connect(PATH_TO_DB);
conn.db.eval('return 5;', (err, response) => {
    //nothing happends
})

这就是我创建连接并调用db.eval的方式,所以我无法使db.eval()正常工作.

That was the way I was creating the connection and calling db.eval, so I couldn't get db.eval() to work.

为了触发db.eval并使它工作,看来您需要显式等待连接:

In order to fire a db.eval and make it work, it seems that you need to wait for the connection explicitly:

const conn = mongoose.connect(PATH_TO_DB, err => {
    mongoose.connection.db.eval('return 5', (err, result) => {
         result; //5!
    })
});

要在尝试调用db.eval之前进行查询也可以:

To make a query before any attemps to call db.eval also works:

const conn = mongoose.connect(PATH_TO_DB);
conn.model(a_collection, a_schema).find({}).exec()
.then(() => {
    conn.db.eval('return 5', (err, result) => {
         result; //5!
    })
})

这篇关于如何通过Mongoose使用mongo db.eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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