Cant create a collection?

node.js mongodb

165 观看


0 作者的声誉

I'm learning node.js with MongoDB. I have installed the complete pack from

I made a node.js file look like this:

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

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");

In the console it shows me that a database is created, but when I open mongo compass and refresh I don't see any database called myDatabase.

So the next thing I tried to is create a collection:

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

MongoClient.connect(url, function(err, db) {
    if(err) throw err;
    db.createCollection("customers", function(err, res) {
        if(err) throw err;
        console.log("Collection created!");

then I get these errors:

Error: Invalid schema, expected `mongodb` or `mongodb+srv`
    at module.exports (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\url_parser.js:15:21)
    at connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:867:3)
    at connectOp (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:253:3)
    at executeOperation (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\utils.js:408:22)
    at MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:244:10)
    at Function.MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:472:22)
    at Object.<anonymous> (D:\Node.js Projects\Node.js MongoDB\CreateCollection\createcollection.js:4:13)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)

Can anyone please help me why I don't see myDatabase in MongoDB Compass UI and why I can't create collection when the previous code says that database is created?

作者: user7896362 的来源 发布者: 2017 年 12 月 27 日

回应 (2)


1086 作者的声誉

The connection url should begin with "mongodb://"

It seems you're using a newer version of the mongodb library that does not match the tutorial you're following

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017";
var dbName = "myDatabase";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;

  var db = client.db(dbName);
  var collection = db.collection("customers");

  // you are using the collection but it won't be created until you insert a
  // document or create an index

  collection.find({}).toArray((err, docs) => {
    if (err) throw err;



It's better to go to the official documentation that matches your library version

作者: lleon 发布者: 27.12.2017 04:36


0 作者的声誉


You can mongoose for creating database as well as collection and documentation

here example :-

var mongoose = request("mongoose");

mongoose.connect('mongodb://localhost:27017/exampleDB', function(err) {
    if (err) {
    } else {
        console.log("database connected");

You get help from this url

作者: user6838417 发布者: 27.12.2017 05:24