Reputation: 73
In my collection I have documents like:
{
"_id": ObjectID("5725290dacb36ae775e25dbe"),
"prop": {
"giver_name": "Random Tester",
"giver_id": ObjectID("56b5e87e1827640215c5c055"),
"giver_url": "random-tester"
},
"owner": ObjectID("568fbeef9823be8e105afa46"),
"title": "testing",
"content": "hgj jhj hgjh gjhgjhgj hgjhg jhg",
"type": "recommendation",
"modified": ISODate("2016-04-30T21:52:13.517Z"),
"created": ISODate("2016-04-30T21:52:13.518Z"),
"privacy": {
"all": true
},
"shares": [],
"interests": [],
"comments": 0,
"acknowledgements": [],
"__v": 0
}
I am trying to fetch the above document with a mongoose query
Post.findAsync({type:'recommendation','prop.giver_id':"56b5e87e1827640215c5c055" }).then(function(count, err){
console.log('Recommendation count: ',count," : ",userId," : ",err);
return res.status(200).json(count);
});
The above query gives me a blank result set, however if I remove the prop.giver_id
part, I get all the results. I know that field is an ObjectId
but as far as I have seen, a string in the find query works pretty well. So what am I doing wrong?
Upvotes: 1
Views: 73
Reputation: 312129
Mongoose will cast the fields in your query based on your schema definition, so when a query like this doesn't work, it's often because the schema definition doesn't match your actual document.
In this case ensure giver_id
is defined in the schema as:
giver_id: mongoose.Schema.ObjectId
Upvotes: 1