-1

I have already installed mongo. it is also working at this port. also contains a database test and collection student.

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

    MongoClient.connect(url, function (err, datbase) {
        if (err) throw err;
        var myStudent = { name: "Jai Sharma", address: "E-3, Arera Colony, Bhopal" };

         db.collection("student").insertOne(myStudent, function (err, result) {
            if (err) throw err;
            console.log("1 Recorded Inserted");
            db.close();
        });

    });

2 Answers2

0

I think this is the relevant line:

MongoClient.connect(url, function (err, datbase)

This says that the database reference should be passed in as a parameter named "datbase". Try changing that from "datbase" to "db", so that the parameter name matches the name used later in the example.

Sean Reilly
  • 21,526
  • 4
  • 48
  • 62
0

If you're using mongodb package version > 3.x callback no longer gives you db reference. Instead it gives you client reference as documented here

So your code should instead be:

MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  // note this line here. This is how you get db reference from client ref
  const db = client.db(dbName);
  ...
1565986223
  • 6,420
  • 2
  • 20
  • 33