简体   繁体   中英

nodejs disconnect mongoose connection after operations

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?

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

//do stuff

db.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM