简体   繁体   中英

How to switch between mysql and mongodb in nodejs

I'm setting up a new nodejs API which will deliver the data on the front end side as soon as the user click a "Search" button. I want to implement a functionality in which user is able to select which db to use ie( Mysql or MongoDB). So how can I switch between both the database in a single API.

Well you can have instances of Mysql and MongoDb both on Node.js side. You will be sending selected db inside the API and based on the selected db you will use the instance. In this case you will have to implement both Mongo and Mysql queries.

if(selectedDb == "MongoDB"){
   // db.collection.find(query, projection)
   // use Mongo Logic here
}else{
    con.connect(function(err) {
      if (err) throw err;
      con.query("SELECT * FROM customers", function (err, result, fields) {
        if (err) throw err;
        console.log(result);
      });
    });
}

I hope it solves your problem.

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