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!')
    }
  });
});

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. Commented Dec 7, 2017 at 9:19
  • Add if (err) { console.log(err); } to log the error & find what's the problem Commented Dec 7, 2017 at 9:28
  • This is because you are using v3.0 of the mongodb driver. The answer is here. Commented Dec 7, 2017 at 13:41
  • Possible duplicate of MongoDB nodeJS error Commented Dec 7, 2017 at 13:42

3 Answers 3

8

It's mongodb version error which you have installed in your project. You should run npm install [email protected] --save in your project

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Great answer, but how can I make it work with newer version of mongodb? (what should I change?)
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); });
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

2 Comments

This is a better answer because of the update to version 3.0. Do you have mongodb documentation on this?
Thank you so much @marnutux finally this solution worked for me after trying many.
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()
   })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.