10

I've been studying mongoose for three days and I'm a bit confused about the use of these two methods (i know that "mongoose.connection()" will be deprecated in the future...)

The problem is: when I'm trying to convert (from "mongoose.connection()" to "mongoose.createConnection()") the action.js file of this example https://gist.github.com/2785463 it seems to not work for me...

there's my code...

var mongoose = require('mongoose'),
db = mongoose.createConnection('localhost', 'test');

db.on('error', function () {
  console.log('Error! Database connection failed.');
});

db.once('open', function (argument) {
  console.log('Database connection established!');

  mongoose.connection.db.collectionNames(function (error, names) {
    if (error) {
      console.log('Error: '+ error);
    } else {
      console.log(names);
    };
  });
});

and there's my terminal output (typing "node test.js" on my ubuntu terminal..)

Database connection established!

/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:437
    throw err;
          ^
TypeError: Cannot call method 'collectionNames' of undefined
  at NativeConnection.<anonymous> (/home/_user_/Scrivania/test2.js:11:25)
  at NativeConnection.g (events.js:192:14)
  at NativeConnection.EventEmitter.emit (events.js:93:17)
  at open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:408:10)
  at NativeConnection.Connection.onOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:415:5)
  at Connection._open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:386:10)
  at NativeConnection.doOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:47:5)
  at Db.open (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:287:14)
  at Server.connect.connectCallback (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:235:7)
  at g (events.js:192:14)
cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
  • [this video](https://www.youtube.com/watch?v=qYlfruqx_S8) is a good reference for connecting to multiple databases using `connect` & `createConnection`. Although it's in Hindi, you can follow along with the code in the video. – Gangula Nov 22 '22 at 18:40
  • similar question is asked here: https://stackoverflow.com/a/53951290/6908282 – Gangula Nov 22 '22 at 18:41

1 Answers1

4

If you don't call mongoose.connect() then mongoose.connection doesn't contain an an open connection. You should be using the return value from your mongo.createConnection() call instead (that you've saved into db).

So the last section of code should change to:

UPDATED

db.db.collectionNames(function (error, names) {
  if (error) {
    console.log('Error: '+ error);
  } else {
    console.log(names);
  };
});

I don't see a collectionNames method on Connection; looks like you have to follow properties down into the native connection object to access that (see above code).

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 1
    well, when i'm trying to use **console.log(db.collections)** it prints an empty object: **{}**, but i'm sure my test db has two collections (i could verify that by the mongo shell using **show collections**). By the way i'll continue with my investigation thx for u help, for now! (and maybe sorry for my bad english :S) – cl0udw4lk3r Nov 01 '12 at 00:17
  • 1
    @cl0udw4lk3r Updated the example now that I found how to access the [`collectionNames`](http://mongodb.github.com/node-mongodb-native/api-generated/db.html#collectionnames) method of the native driver's `Db` object from a Mongoose connection. – JohnnyHK Nov 01 '12 at 12:31
  • Nice! It works! Thanks a lot, maybe i should change the line **_db_ = mongoose.createConnection(...)** to **_connection_ = mongoose.createConnection(...)** to avoid mistakes. – cl0udw4lk3r Nov 01 '12 at 13:56
  • @JohnnyHK, Im using the exact code as you have mentioned above, but the callback function is never getting called. If i restart the mongod process, the callback is automatically called. Why is the collectionsNames callback not called? – Rajkamal Subramanian Jul 30 '13 at 03:37
  • @rajkamal It's best to post that as a new question that includes your own code showing how you're making the connection. – JohnnyHK Jul 30 '13 at 13:45