MongoDB / Express - How to switch database after connecting via connect()

前端 未结 2 1031
时光说笑
时光说笑 2021-01-03 06:26

I am using express to connect to my mongoDB:

mongodb.MongoClient.connect(mongourl, function(err, database) {

      // How would one switch to another databa         


        
2条回答
  •  天命终不由人
    2021-01-03 06:57

    You just have to call MongoClient.connect once again, because there is one connection per database. That means, you cannot change the database of an existing connection. You have to connect a second time:

    mongodb.MongoClient.connect(mongourl, function(err, database) {
        mongodb.MongoClient.connect(mongourl_to_other_database, function(err, database2) {
            // use database or database2
        });
    });
    

提交回复
热议问题