I have questions objects like below when populate it.
{
title: "Question Title",
category: {
"_id": "562ddc95a93f89e012554be1",
"name": "Category",
"parent": {
"_id" : "562ddc95a93f89e012554be1",
"name": "Parent Category"
}
}
}
And category objects like below
{
"_id" : "562ddc95a93f89e012554be1",
"name": "Category Name",
"parent": "562ddc95a93f89e012554be1"
}
I want get questions count which in specific parent category.
I try a query like this but it's not working.
Questions
.count("category.parent._id":_id)
How can i do this? Is this possible in mongoose?
You could structure your use of count like this:
Questions.count({"category.parent._id" : _id}, function(err, c){
if(err) return err;
console.log('Count is ' + c);
});
See the various possibilities in the Docs.
To test this in the mongodb shell, I created the following two documents in a collection called questions.
{
"title" :"Question1",
"category":{
"_id":"123",
"name" : "category 1",
"parent" : {
"_id" : "567",
"name" : "parent category"
}
}
},
{
"title" :"Question2",
"category":{
"_id":"124",
"name" : "category 1",
"parent" : {
"_id" : "568",
"name" : "parent category"
}
}
}
This query should return 1:
db.questions.count({"category.parent._id":"568"})
you should place the criteria in double quotes. It may produce an error if not used.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments