Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Devops
  3. Question
Devops

db.collection is not a function when using MongoClient v3.0

Asked by Aishwarya Jhadav Jun 27, 2021 1.2K views 1 answer
Share

About this question

When I try to implement this example in a nodeJS environment and invoke the function with an AJAX call, I got the error below:

TypeError: db.collection is not a function 
at c:UsersuserDesktopWeb ProjectWebService.JS:79:14 
at args.push (c:Usersusernode_modulesmongodblibutils.js:431:72) 
at c:Usersusernode_modulesmongodblibmongo_client.js:254:5 at connectCallback (c:Usersusernode_modulesmongodblibmongo_client.js:933:5) at c:Usersusernode_modulesmongodblibmongo_client.js:794:11 at _combinedTickCallback (internal/process/next_tick.js:73:7) 
at process._tickCallback (internal/process/next_tick.js:104:9)

Please find below my implemented code:

var MongoClient = require('mongodb').MongoClient; 
var url = "mongodb://localhost:27017/mytestingdb";
MongoClient.connect(url, function(err, db) { 
if (err) throw err; 
db.collection("customers").findOne({}, 
function(err, result) { 
if (err) throw err; 
console.log(result.name); 
db.close(); 
}); 
});

Note that the error occurs whenever the execution hits:

db.collection("customers").findOne({}, function(err, result) {}

Also, note (in case it matters) that I have installed the latest MongoDB package for node JS (npm install MongoDB), and the MongoDB version is MongoDB Enterprise 3.4.4, with MongoDB Node.js driver v3.0.0-rc0.

Your answer

1 Answer

More Devops discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest DevOps Blogs

Guides, tips and career advice on DevOps from JanBask experts.