【问题标题】:How to switch between mysql and mongodb in nodejsnodejs如何在mysql和mongodb之间切换
【发布时间】:2020-02-07 10:01:54
【问题描述】:

我正在设置一个新的 nodejs API,一旦用户单击“搜索”按钮,它将在前端传递数据。我想实现一个功能,用户可以选择使用哪个数据库,即(Mysql 或 MongoDB)。那么如何在一个 API 中切换两个数据库。

【问题讨论】:

    标签: mysql node.js mongodb


    【解决方案1】:

    你可以在 Node.js 端同时拥有 Mysql 和 MongoDb 的实例。您将在 API 中发送选定的数据库,并根据选定的数据库使用实例。在这种情况下,您必须同时实现 Mongo 和 Mysql 查询。

    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);
          });
        });
    }
    

    希望它能解决您的问题。

    【讨论】:

    • 我会尝试这种方法。感谢您的帮助
    猜你喜欢
    • 2015-10-03
    • 2012-09-12
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多