172

I have a large collection of 300 question objects in a database test. I can interact with this collection easily through MongoDB's interactive shell; however, when I try to get the collection through Mongoose in an express.js application I get an empty array.

My question is, how can I access this already existing dataset instead of recreating it in express? Here's some code:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/test');
mongoose.model('question', new Schema({ url: String, text: String, id: Number }));

var questions = mongoose.model('question');
questions.find({}, function(err, data) { console.log(err, data, data.length); });

This outputs:

null [] 0
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
theabraham
  • 15,840
  • 9
  • 42
  • 41

9 Answers9

295

Mongoose added the ability to specify the collection name under the schema, or as the third argument when declaring the model. Otherwise it will use the pluralized version given by the name you map to the model.

Try something like the following, either schema-mapped:

new Schema({ url: String, text: String, id: Number}, 
           { collection : 'question' });   // collection name

or model mapped:

mongoose.model('Question', 
               new Schema({ url: String, text: String, id: Number}), 
               'question');     // collection name
calvinfo
  • 2,966
  • 1
  • 15
  • 4
  • 13
    Where in the docs can i find this information? This really helpped but there's no place explaining the plural thing. – StudioWorks Apr 07 '14 at 20:31
  • Hi, @calvinfo how can I change the collection name at runtime? I have 5 collections of UserSchema and I want to give each one a different name Eg: users_server1, users_server2, users_server3... – Ragnar Jun 06 '14 at 20:41
  • 1
    Pleae provide example query, e.g. with `Model.collection.insert();..` – Stephan Kristyn Sep 17 '14 at 15:39
  • This does not help me. I have a collection called "widget_data" that I am trying to access (built somewhere else), but when I put var WidgetData = mongoose.model('WidgetData', new Schema(), 'widget_data'); then try to do WidgetData.find() it comes up with nothing. – kungfuspider Apr 08 '15 at 20:32
  • 9
    Same here, I spend many hours figuring out this issue, the document is found here http://mongoosejs.com/docs/guide.html#collection – ElvinD Sep 16 '15 at 06:50
  • 5
    This worked for me. I had a users collection that I mongorestored. To get access to it with mongoose I did `mongoose.connect("mongodb://localhost/fromlab"); var Schema = mongoose.Schema; var User = mongoose.model("User", new Schema({}), "users"); User.find({}, function(err, doc){ console.log((doc)) })` – jack blank Sep 21 '16 at 03:04
  • I tried the same `@jack blank`, but now i get a error saying `Can not recompile already modelled data`. :( – Mr.Bhat Dec 26 '18 at 15:42
74

Here's an abstraction of Will Nathan's answer if anyone just wants an easy copy-paste add-in function:

function find (name, query, cb) {
    mongoose.connection.db.collection(name, function (err, collection) {
       collection.find(query).toArray(cb);
   });
}

simply do find(collection_name, query, callback); to be given the result.

for example, if I have a document { a : 1 } in a collection 'foo' and I want to list its properties, I do this:

find('foo', {a : 1}, function (err, docs) {
            console.dir(docs);
        });
//output: [ { _id: 4e22118fb83406f66a159da5, a: 1 } ]
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
Michael Taufen
  • 1,704
  • 1
  • 17
  • 22
45

You can do something like this, than you you'll access the native mongodb functions inside mongoose:

var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/local');

var connection = mongoose.connection;

connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function () {

    connection.db.collection("YourCollectionName", function(err, collection){
        collection.find({}).toArray(function(err, data){
            console.log(data); // it will print your collection data
        })
    });

});

Update 2022

If you get an MongoInvalidArgumentError: The callback form of this helper has been removed. error message, here's the new syntax using async/await:

const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/productsDB');

const connection = mongoose.connection;

connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', async function () {

  const collection  = connection.db.collection("Products");
  collection.find({}).toArray(function(err, data){
      console.log(data); // it will print your collection data
  });

});
Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
Leo Ribeiro
  • 1,195
  • 1
  • 14
  • 25
17

I had the same problem and was able to run a schema-less query using an existing Mongoose connection with the code below. I've added a simple constraint 'a=b' to show where you would add such a constraint:

var action = function (err, collection) {
    // Locate all the entries using find
    collection.find({'a':'b'}).toArray(function(err, results) {
        /* whatever you want to do with the results in node such as the following
             res.render('home', {
                 'title': 'MyTitle',
                 'data': results
             });
        */
    });
};

mongoose.connection.db.collection('question', action);
Will Nathan
  • 625
  • 7
  • 13
  • 1
    This is exactly what I was looking for because mongoose doesn't have any gridFS support. I use this method to grab file metadata from gridfs (gridstore). Just replace `question` in the code above with `fs.files` and you're good to go. – k00k Jun 05 '12 at 13:01
7

Are you sure you've connected to the db? (I ask because I don't see a port specified)

try:

mongoose.connection.on("open", function(){
  console.log("mongodb is connected!!");
});

Also, you can do a "show collections" in mongo shell to see the collections within your db - maybe try adding a record via mongoose and see where it ends up?

From the look of your connection string, you should see the record in the "test" db.

Hope it helps!

busticated
  • 2,147
  • 1
  • 20
  • 20
  • 4
    Interesting, it's actually storing the information in a `questions` collection when the data I'm trying to access is in a `question` collection. Does Mongoose automatically pluralize collection/model names? – theabraham Apr 27 '11 at 06:34
  • Yeah I think it does... ha! I'm just getting started myself so I haven't explored all the nooks and crannies... but I recall seeing that li'l chestnut breeze by as I was spinning through the Google Groups. – busticated Apr 27 '11 at 21:12
6

Something else that was not obvious, to me at least, was that the when using Mongoose's third parameter to avoid replacing the actual collection with a new one with the same name, the new Schema(...) is actually only a placeholder, and doesn't interfere with the exisitng schema so

var User = mongoose.model('User', new Schema({ url: String, text: String, id: Number}, { collection : 'users' }));   // collection name;
User.find({}, function(err, data) { console.log(err, data, data.length);});

works fine and returns all fields - even if the actual (remote) Schema contains none of these fields. Mongoose will still want it as new Schema(...), and a variable almost certainly won't hack it.

Bart
  • 61
  • 1
  • 1
  • 1
    it gives 'collection name must be string' error to me, Edit : as 'calvinfo' s answer, If you want to give collection name in model constructor you just pass the collection name in string form not object model. so your answer will be true if edit like this, var User = mongoose.model('User', new Schema({ url: String, text: String, id: Number}, 'users')); // collection name; User.find({}, function(err, data) { console.log(err, data, data.length);}); – Kaan Erkoç May 01 '17 at 09:40
1

Go to MongoDB website, Login > Connect > Connect Application > Copy > Paste in 'database_url' > Collections > Copy/Paste in 'collection' .

var mongoose = require("mongoose");
mongoose.connect(' database_url ');
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function () {
  conn.db.collection(" collection ", function(err, collection){
    collection.find({}).toArray(function(err, data){
      console.log(data); // data printed in console
    })
  });
});
turivishal
  • 34,368
  • 7
  • 36
  • 59
rttss_sahil
  • 235
  • 4
  • 10
1

I tried all the answers but nothing worked out, finally got the answer hoe to do it.

var mongoose = require('mongoose');
mongoose.connect('mongodb://0.0.0.0:27017/local');

// let model = require('./test1');

setTimeout(async () => {
  
    let coll = mongoose.connection.db.collection(<Your collection name in plural form>);
    // let data = await coll.find({}, {limit:2}).toArray();
    // let data = await coll.find({name:"Vishal"}, {limit:2}).toArray();
    // let data = await coll.find({name:"Vishal"}, {projection:{player:1, _id:0}}).toArray();
    let data = await coll.find({}, {limit:3, sort:{name:-1}}).toArray();
    console.log(data);
    
}, 2000);

I have also mentioned some of the criteria to filter out. Delete and update can also be done by this.

Thanks.

Vishal
  • 99
  • 1
  • 5
0

Make sure you're connecting to the right database as well as the right collection within the database.

You can include the name of the database in the connection string.

notice databasename in the following connection string:

var mongoose = require('mongoose');
const connectionString = 'mongodb+srv://username:password@hosturl.net/databasename';

mongoose.connect(connectionString);
David
  • 895
  • 8
  • 12