3

When I run the script, the error returns.

TypeError: db.collection is not a function

 var MongoClient = require('mongodb').MongoClient;
 var url = "mongodb://abc:12345**@xxxx.mlab.com:&&&&/myDB";
 MongoClient.connect(url, function(err, db) {
   if(err) {
      console.log(err);
   } else {
      console.log("Database created!");
      db.collection('users').aggregate([{
        '$match': {
          'organization.organizationId': "e1716c62-fdce-11e7-8be5-
           0ed5f89f718b"
        }
      },{
       "$project": {
          "deviceDetails": 1,
          "userDetails": 1
       }
     }], function(error, documents) {
           if (error) {
             console(error);
           } else {
           console.log(documents);
          }
        });
  });

Hi, could you please help me where am I doing wrong.Thanks!

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
Rahul Saini
  • 927
  • 1
  • 11
  • 28
  • 1
    Possible duplicate of [TypeError: db.collection is not a function](https://stackoverflow.com/questions/43779323/typeerror-db-collection-is-not-a-function) – Saravana Jan 20 '18 at 12:02
  • I believe that you're using Mongo Driver 3.0 (or above) for Node.js? In that case you should not be getting a db, but a client as callback. – Nilesh Singh Jan 20 '18 at 12:24
  • @NileshSingh Yes I have a mongoVersion 3.6.1, plz tell how to get 'db' in the about script – Rahul Saini Jan 20 '18 at 13:30

3 Answers3

4

With Mongo Driver 3.0 or above, the connect callback returns err and client instead of db. To get the db out of the client do this,

var db = client.db;

In your case, it will look something like this,

MongoClient.connect(url, function(err, client) {
   if(err) {
    console.log(err);
   } else {
      var db = client.db;
      console.log("Database created!");
      db.collection('users').aggregate(...)
   }
})
Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
2

Use MongoClient.connect(url, function(err, client)) where this was released in recent updates. For more info refer Mongo Driver Docs.

var MongoClient = require('mongodb').MongoClient;
// Dont use database name in link
var url = "mongodb://abc:12345**@xxxx.mlab.com:&&&&";
MongoClient.connect(url, function(err, client) {
    if (err) {
        console.log(err);
    } else {
        let db = client.db('myDB')
        console.log("Database created!");
        db.collection('users').aggregate([{
            '$match': {
                'organization.organizationId': "e1716c62-fdce-11e7-8be5-0e d5f89f718b "
            }
        }, {
            "$project": {
                "deviceDetails": 1,
                "userDetails": 1
            }
        }], function(error, documents) {
            if (error) {
                console(error);
            } else {
                console.log(documents);
            }
        });
    }
});
Sivanesh S
  • 1,117
  • 10
  • 16
0

First you should check that database connect or not only run this code so that this confirm that variable url is correct or mongodb is correctly installed.

MongoClient.connect(url, function (err, db) {         
 if (err)

          {
              console.log("Failed connecting to the database.  " + err);
          } 
          else
          {
           console.log(Sucessfully)

           }
});
Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
s.babar
  • 408
  • 3
  • 10