How can I connect to mongodb using express without mongoose?

后端 未结 2 1805
情话喂你
情话喂你 2021-02-04 16:26

I am using the express framework and would like to connect to a mongodb without using mongoose, but with the native nodejs Mongodb driver. How can I do this without creating a n

2条回答
  •  迷失自我
    2021-02-04 16:38

    var mongodb = require('mongodb');
    var uri = 'mongodb://localhost:27017/dbname';
    
    module.exports = function(callback) {
      mongodb.MongoClient.connect(uri, callback);
    };
    

    Ad this snippet in a file say connect.js and then require this file(connect.js) in your file where you are declaring your functions for http requests.

提交回复
热议问题