DeprecationWarning: current URL string parser is deprecated when Node.js connects to MongoDB database

Learn to use Node.js to connect to MongoDB database today, this is my original code

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("one");
    var myobj = { name: "jack", age: "13" };
    dbo.collection("site").insertOne(myobj, function(err, res) {
        if (err) throw err;
        console.log("文档插入成功");
        db.close();
    });
});

Running error:
insert image description here
Literally, this parser is a bit outdated? We should use a new method to move { useNewUrlParser: true } to the parameters of MongoClient.connect.
After the change:

MongoClient.connect(url,{ useNewUrlParser: true }, function(err, db) {

});

This will run without error.

Related Posts