2

When I run this code it has a Error 'TypeError: db.collection is not a function'. How to solve this problem and where is wrong in this code.

const MongoClient = require('mongodb').MongoClient;

// Connection url
const url = 'mongodb://localhost:27017/blog';

// Connect using MongoClient
MongoClient.connect(url, (err, db) => {
  const note = {
    text: 'Note content text',
    title: 'Note title'
  };
  
  db.collection('notes').insert(note, (err, result) => {
    if (err) {
      console.log('An error has occurred!')
    } else {
      console.log('Insert success!')
    }
  });
});
Edwin
  • 33
  • 1
  • 4
  • 1
    You should add a `if (err) { console.log('Could not connect'); }` in the callback when you connect. It may be the case that it can't connect and that is why db doesn't have the collection function set up. – maxpaj Dec 07 '17 at 09:19
  • Add `if (err) { console.log(err); }` to log the error & find what's the problem – Vishant dhandha Dec 07 '17 at 09:28
  • This is because you are using v3.0 of the mongodb driver. The answer is [here](https://stackoverflow.com/a/47662979/8574934). – Mika Sundland Dec 07 '17 at 13:41
  • Possible duplicate of [MongoDB nodeJS error](https://stackoverflow.com/questions/47662220/mongodb-nodejs-error) – Mika Sundland Dec 07 '17 at 13:42

3 Answers3

8

It's mongodb version error which you have installed in your project. You should run npm install mongodb@2.2.5 --save in your project

Taimoor
  • 96
  • 3
  • Installed mongodb server version: `v3.2.18` mongodb npm version: `^3.0.0-rc0` Can you please advice where can i find proper documentation with connection code examples? I am also facing same error. – Amit Shah Dec 08 '17 at 10:45
  • found the answer here : https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0?noredirect=1&lq=1 – Amit Shah Dec 08 '17 at 10:54
  • Great answer, but how can I make it work with newer version of mongodb? (what should I change?) – Pini Jul 11 '18 at 11:28
  • mongoose.Promise = global.Promise; mongoose.connect('mongodb://localhost/test'); mongoose.connection.on('error', () => { console.log('MongoDB connection error. Please make sure MongoDB is running.'); process.exit(1); }); – Taimoor Jul 31 '18 at 11:31
7

For people on version 3.0 of the MongoDB native NodeJS driver:

In version 2.x of the MongoDB native NodeJS driver you would get the database object as an argument to the connect callback:

MongoClient.connect('mongodb://localhost:27017/blog', (err, db) => {
  // Database returned
});

According to the changelog for 3.0 you now get a client object containing the database object instead:

MongoClient.connect('mongodb://localhost:27017', (err, client) => {
  // Client returned
  var db = client.db('blog');
});

Original Answer: https://stackoverflow.com/a/47662979/2272082

marnutux
  • 75
  • 1
  • 6
1

check out with this one it might help you out to get closer to your needy solution.

const url = 'mongodb://localhost:27017/learnyoumongo'

var mongo = require('mongodb').MongoClient

mongo.connect(url, function(err, client) {
      if(err) {
         throw err;
      }
      var collection = client.db('learnyoumongo').collection('parrots');
        collection.find({
          age : {
            $gt : +process.argv[2]
          }
        }).toArray(function(err, documents) {
        console.log(documents)
        client.close()
   })
})