完成后,正确关闭猫鼬的连接

时间:2023-02-06 02:32:39

I'm using mongoose in a script that is not meant to run continuously, and I'm facing what seems to be a very simple issue yet I can't find an answer; simply put once I make a call to any mongoose function that sends requests to mongodb my nodejs instance never stops and I have to kill it manually with, say, Ctrl+c or Program.exit().

我在一个脚本中使用了mongoose,它不是连续运行的,我面对的似乎是一个非常简单的问题,但是我找不到答案;只要调用某个mongoose函数,该函数向mongodb发送请求,我的nodejs实例就永远不会停止,我必须手动终止它,例如,Ctrl+c或Program.exit()。

The code looks roughly like this:

代码大致如下:

var mongoose = require('mongoose');

// if my program ends after this line, it shuts down as expected, my guess is that the connection is not really done here but only on the first real request ?
mongoose.connect('mongodb://localhost:27017/somedb'); 

// define some models

// if I include this line for example, node never stop afterwards
var MyModel =  mongoose.model('MyModel', MySchema);

I tried adding calls to mongoose.disconnect() but no to result. Aside from that, everything works fine (finding, saving, ...).

我尝试向mongoose.disconnect()添加调用,但是没有结果。除此之外,一切都运行良好(查找、保存、…)。

This is the exact same issue as this person, sadly he did not receive any answer: https://groups.google.com/group/mongoose-orm/browse_thread/thread/c72cc1c51c76e661

这和这个人的问题完全相同,遗憾的是他没有得到任何答案:https://groups.google.com/group/mongoose-orm/browse_thread/thread/c72cc1c51c76e661

Thanks

谢谢

EDIT: accepted the answer below as it is technically correct, but if anyone ever hit this problem again, it seems that mongoose and/or the mongodb driver does not actually close the connection when you ask it to if there are still queries running.

编辑:接受下面的答案,因为它在技术上是正确的,但是如果有人再次遇到这个问题,似乎mongoose和/或mongodb驱动程序并没有真正地关闭连接,如果仍然有查询在运行的话。

It does not even remember the disconnect call at all, it does not do it once queries are finished running; it just discards your call with no exception thrown or anything of the sort, and never actually close the connection.

它甚至根本不记得断开连接的调用,一旦查询完成,它也不会这样做;它只是丢弃您的调用,没有抛出任何异常或类似的任何东西,并且永远不会真正关闭连接。

So there you have it: make sure that every query has been processed before calling disconnect() if you want it to actually work.

这里有:如果您想让每个查询实际工作,请确保每个查询在调用disconnect()之前都经过了处理。

5 个解决方案

#1


135  

You can close the connection with

您可以使用

mongoose.connection.close()

#2


42  

The other answer didn't work for me. I had to use mongoose.disconnect(); as stated in this answer.

另一个答案对我不起作用。我必须使用mongoose。disconnect();如这个答案所述。

#3


7  

You can set the connection to a variable then disconnect it when you are done:

您可以将连接设置为一个变量,然后在完成时断开连接:

var db = mongoose.connect('mongodb://localhost:27017/somedb');

// Do some stuff

db.disconnect();

#4


1  

I'm using version 4.4.2 and none of the other answers worked for me. But adding useMongoClient to the options and putting it into a variable that you call close on seemed to work.

我使用的是4.4.2版本,其他答案对我都不起作用。但是将useMongoClient添加到选项中,并将其放入一个您所调用的变量中,这似乎起作用了。

var db = mongoose.connect('mongodb://localhost:27017/somedb', { useMongoClient: true })

//do stuff

db.close()

#5


0  

you will get error if you try close/disconnect outside of then method. Better solution is close the connection in both callback in then method.The dummy code is here.

如果你尝试在方法之外关闭/断开连接,就会出现错误。更好的解决方案是在then方法中关闭两个回调中的连接。伪代码在这里。

var newTodo=new Todo({text:'cook dinner'});
newTodo.save().then((docs)=>{
  console.log('todo saved',docs);
 mongoose.connection.close();//mongoose.disconnect(); also work and prefered
},(e)=>{
  console.log('unable to save');

});
  

#1


135  

You can close the connection with

您可以使用

mongoose.connection.close()

#2


42  

The other answer didn't work for me. I had to use mongoose.disconnect(); as stated in this answer.

另一个答案对我不起作用。我必须使用mongoose。disconnect();如这个答案所述。

#3


7  

You can set the connection to a variable then disconnect it when you are done:

您可以将连接设置为一个变量,然后在完成时断开连接:

var db = mongoose.connect('mongodb://localhost:27017/somedb');

// Do some stuff

db.disconnect();

#4


1  

I'm using version 4.4.2 and none of the other answers worked for me. But adding useMongoClient to the options and putting it into a variable that you call close on seemed to work.

我使用的是4.4.2版本,其他答案对我都不起作用。但是将useMongoClient添加到选项中,并将其放入一个您所调用的变量中,这似乎起作用了。

var db = mongoose.connect('mongodb://localhost:27017/somedb', { useMongoClient: true })

//do stuff

db.close()

#5


0  

you will get error if you try close/disconnect outside of then method. Better solution is close the connection in both callback in then method.The dummy code is here.

如果你尝试在方法之外关闭/断开连接,就会出现错误。更好的解决方案是在then方法中关闭两个回调中的连接。伪代码在这里。

var newTodo=new Todo({text:'cook dinner'});
newTodo.save().then((docs)=>{
  console.log('todo saved',docs);
 mongoose.connection.close();//mongoose.disconnect(); also work and prefered
},(e)=>{
  console.log('unable to save');

});