I have setup a remote MongoDB server with Mongolab and my intent is to do basic read and write operations. I successfully connect to the database but a call to db.collectionNames() returns a null object. I have one collection at the moment and I'm expecting it to return the collection name only.
In the code below I then created a dummy collection collection1 just to see if the call to db.collectionNames() would return collection1, but I still get a null object. My questions are:
Does a connection to a remote database automatically make all the collections it contains available via db.collectionName() calls? Or..
Do I need to create a separate collection object for each collection I have within my database?(note: the database and collections have already been setup with data on Mongolab website). In the documentation db.collection and db.collections are available but in my code they both return a null object when passed to console.log() via a callback.
How do I need to change the code below in order to read/write from a specific collection, and query the database for the names of all the collections it contains?
var express = require('express');
var router = express.Router();
var mongoClient = require('mongodb').MongoClient;
mongoClient.connect("mongodb://user:pwd@ds033750.mongolab.com:33750/dbName", function(err, db) {
if(!err) {
console.log("We are connected");
}
var collection1 = db.collection('collection1');
db.collections(function(err, names){
if(err) {
console.log("We do not have a collection");
}
console.log(names);
});