22

I can't seem to get a response from mongodb. I am using node.js and mongodb with the help of mongoose.

In my node.js app I have

mongoose.connect('mongodb://localhost:27017/myDB');

var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var BlogPost = new Schema({
    author  : ObjectId,
    title   : String,
    slug    : { type: String, lowercase: true, trim: true },
    content : String,
    summary : String,
    date    : Date
})

var BlogModel = mongoose.model('BlogPost', BlogPost);

BlogModel.find({}, function(docs){
   console.log(docs);
});

If I type show dbs in the mongo shell I get

admin   (empty)
myDB       0.203125GB
local   (empty)
test    (empty)

db.blogmodel.find() returns :

{ "_id" : ObjectId("50108d3df57b0e3375a20479"), "title" : "FirstPost" }

and yes I do have mongod running.

Fixed Solution

var BlogModel = mongoose.model('blogmodel', BlogPost, 'blogmodel');

It works because its (model name, schema name, collection name)

1
  • 5
    This issue has driven me crazy! I added credit for your fixed solution part! Commented Jan 9, 2015 at 7:44

4 Answers 4

43

Mongoose pluralizes model names so it's running find on the "blogposts" collection instead of "blogpost". That said, your query in the mongo shell is on the "blogmodel" collection. In that case:

var BlogModel = mongoose.Model("BlogModel", ..)

or pass the collection name as the third param:

var BlogModel = mongoose.model("BlogPost", schema, "blogmodel")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, I had to change it to the above fix but your post helped me solve it so i'll give you the answer.
Thank you! Why isn't this in the mongoose docs? mongoosejs.com/docs/models.html
6

The first parameter to your BlogModel.find callback is err, the second parameter is docs. So your code should be:

BlogModel.find({}, function(err, docs){
   console.log(docs);
});

2 Comments

Does the BlogPosts collection contain documents if you query it from the mongo shell?
I added it to the original post and yes I have one document for testing, Which I can't find for some reason.
1

For anyone still experiencing this issue even after checking the accepted answer – make sure your database connection url contains the database name, e.g. for database name myDB...

mongoose.connect(`mongodb://localhost:27017/myDB`)

I'd forgotten it and my mongoose searches were all returning null or empty arrays.

Comments

0

I experienced similar error yesterday, in my case error was caused by data imported to mongo. After I used mongoimport key _id was stored as string instead of ObjectId. When I was querying data in mongo everything works well, but in Mongoose when I was trying find something by _id it always returned null or empty Array. I hope that info might by useful for someone.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.