I'm trying to query documents in MongoDB
using findOne()
, but it's not working. The field I'm trying to filter by is 'date', and I'm not sure if perhaps it's a special word I shouldn't be using when inserting docs. Is there something I'm missing?
Mongo Docs:
{ "_id" : ObjectId("52c271a5d1344a7a326c0d48"), "author" : "User", "title" : "Fourth post", "body" : "This is the fourth post", "date" : "1000000" }
Node:
var postSchema = mongoose.Schema({ author:String, title:String, body:String, date:Number, attachments:Array }), post = mongoose.model('post', postSchema); exports.getPost = function(id, callback) { console.log(id); post.findOne({date:id}, function(err, item) { console.log(item); }); }
Console: (querying http://localhost:8080/posts/1000000
)
1000000 null
{ "date" : "1000000" }
in your Mongo doc seems suspect. Since it's a number, it should be { date : 1000000 }
It's probably a type mismatch. Try post.findOne({date: "1000000"}, callback)
and if that works, you have a typing issue.
Source: Stackoverflow.com