Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose JS queries all coming back null or empty

I am trying to create a simple MongooseJS example program that gets a list of items from a collection, and it's coming back empty every time. Here is the code:

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

var sampleSchema = new Schema({
    sampleField    : String
});

var db = mongoose.connect('mongodb://localhost:27017/test');

var sampleCollection = mongoose.model('sampleCollection', sampleSchema);

sampleCollection.find({ } , function (err, items) {
    console.log(items); // outputs []
    console.log(err); // outputs null
    items.forEach( function(item) {
        console.log(item); // does not reach this code
    });
});

I have a default instance of MongoDB running, and this is what I've entered in the shell:

> use test
> db.sampleCollection.save({sampleField : "Hello"});
> db.sampleCollection.save({sampleField : "Goodbye"});
> db.sampleCollection.find({});
{ "_id" : ObjectId("4f28944b38b59225012109da"), "sampleField" : "Hello" }
{ "_id" : ObjectId("4f28945138b59225012109db"), "sampleField" : "Goodbye" }

Any idea why my code doesn't return any data?

Thanks for your help, Dave

like image 866
Dave Morris Avatar asked Feb 01 '12 01:02

Dave Morris


People also ask

Can Mongoose find return null?

All callbacks in Mongoose use the pattern: callback(error, result) . If an error occurs executing the query, the error parameter will contain an error document, and result will be null. If the query is successful, the error parameter will be null, and the result will be populated with the results of the query.

What does Mongoose find return?

find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. New! Save questions or answers and organize your favorite content. Learn more.

Does Mongoose save return a promise?

save() is a method on a Mongoose document. The save() method is asynchronous, so it returns a promise that you can await on.


2 Answers

mongoose will normalize the name of collection to lowercase and pluralzed. Therefore, you should insert into db.samplecollections instead of db.sampleCollection. (Notice the difference of letter c and s here).

to test it:

s = new sampleCollection({sampleField: 'hello'}); // creates a new record
s.save(function(err) { 
  sampleCollection.find({ } , function (err, items) {
      console.log(items); 
      console.log(err); 
      items.forEach( function(item) {
          console.log(item); 
      });
  });
});

and it properly prints:

[ { sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 } ]
null
{ sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 }

then in mongo shell:

> show collections
samplecollections          //<<<<<<<<<<<<<< It's all lowercase and pluralized
system.indexes

> db.samplecollections.find()
{ "sampleField" : "hello", "_id" : ObjectId("4f28ab4cc9e58f710a000001") }
like image 82
qiao Avatar answered Nov 21 '22 20:11

qiao


While this is true, you can specify the name of the collection in the third argument and it will use the case from that string:

var sampleCollection = mongoose.model('sampleCollection', sampleSchema,'SampleCollection');
like image 40
gregsaab Avatar answered Nov 21 '22 22:11

gregsaab