Reputation: 7008
I have the documents as follows:
{
"_id": {
"$oid": "5bf662551a0ffc2f40c91945"
},
"categories": [
"angular"
],
"title": "1",
"details": "1",
"url": "undefined",
"category": {
"$oid": "5bf135657d6f2c69a527186d"
},
"created_at": {
"$date": "2018-11-22T08:01:25.418Z"
},
"__v": 0
}
I want to find all documents based on category id. I tried this:
articleModel
.find({category: req.params.id})
.sort({ $natural: -1 })
.exec(function(err, blogs) {
if (err) {
throw err;
}
console.log("get blogs by category ", blogs);
res.json({
success: true,
message: "blogs by category",
blogs: blogs
});
});
but it returns empty array.
Upvotes: 0
Views: 56
Reputation: 13669
use new mongoose.Types.ObjectId(YOUR_ID)
when find using mongoose object ID :
articleModel
.find({"category": new mongoose.Types.ObjectId(req.params.id)})
.exec(function(err, blogs) {
if (err) {
throw err;
}
console.log("get blogs by category ", blogs);
res.json({
success: true,
message: "blogs by category",
blogs: blogs
});
});
Upvotes: 1