Reputation: 461
I have the following mongoose script to connect to the local db and do some operations. But i have problem disconnecting it.
const mongoose = require('mongoose');
const db = mongoose.connect(`mongodb://localhost/mydb`);
const userModel = {
country: { type: String }
newField: { type: String }
};
const myUsersModel = mongoose.model('user',userModel);
myUsersModel.find({country:"USA"})
.then(users => users.forEach(function (doc) {
// some operations
doc.save();
db.disconnect();
}));
the problem is that the script doesn't disconnect the mongoose connection. Could somebody help to fix this?
Upvotes: 0
Views: 132
Reputation: 395
var db = mongoose.connect('mongodb://localhost:27017/somedb', { useMongoClient: true })
//do stuff
db.close()
Upvotes: 1