Can I get rows count by populated property in mongoose query?

Furkan Başaran

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?

Abdullah Rasheed

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In MongoDB with Mongoose (NodeJS) how can I query(find) over a populated field?

From Dev

Mongodb / Mongoose: Can I sort a parent query by a populated child document's created date?

From Dev

How can I get the count of multiple table rows storing in different variables using single query?

From Dev

Query populated document with mongoose

From Dev

How do I obtain the count of populated children in a Mongoose model?

From Dev

How can I count the numbers of rows that a phalcon query returned?

From Dev

How can I get a count of rows that match a given year in Rails?

From Dev

How can I get the count of the visible rows of a grid?

From Dev

How can I get a count of rows that match a given year in Rails?

From Dev

How can I get the count of the amount of rows for this result set in php?

From Dev

Is there a way that I can get a count of rows with a LINQ statement?

From Dev

How can I get count of rows for every week?

From Java

Mongodb mongoose query get count of documents by reference

From Dev

How to get data and count in one query in mongoose

From Dev

how can i write as one query to get count in mysql

From Dev

How can I make a query in mongoose and node.js to count users depending on a value (similar to mysql distinct)?

From Dev

How can I get the average of the top N rows of a SQL query?

From Dev

How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

From Dev

Get total count of rows in pagination query

From Dev

get whole count of rows in a query with group condition

From Dev

Node - Mongoose 3.6 - Sort query with populated field

From Dev

Can't get rows for zero values in SQL query with COUNT and 3 JOINs (4 tables)

From Dev

How can I query with elemMatch in mongoose?

From Dev

How can I get a populated combobox to display its options in Swift?

From Dev

How can I get dropdown options populated from state?

From Dev

Can I have a LINQ query that counts total of rows filtered with a where on a property?

From Dev

Mongoose aggregate: How do I get all document that does not have a column populated?

From Dev

How I can count identical rows in sql

From Dev

How can I count matched rows individually?

Related Related

  1. 1

    In MongoDB with Mongoose (NodeJS) how can I query(find) over a populated field?

  2. 2

    Mongodb / Mongoose: Can I sort a parent query by a populated child document's created date?

  3. 3

    How can I get the count of multiple table rows storing in different variables using single query?

  4. 4

    Query populated document with mongoose

  5. 5

    How do I obtain the count of populated children in a Mongoose model?

  6. 6

    How can I count the numbers of rows that a phalcon query returned?

  7. 7

    How can I get a count of rows that match a given year in Rails?

  8. 8

    How can I get the count of the visible rows of a grid?

  9. 9

    How can I get a count of rows that match a given year in Rails?

  10. 10

    How can I get the count of the amount of rows for this result set in php?

  11. 11

    Is there a way that I can get a count of rows with a LINQ statement?

  12. 12

    How can I get count of rows for every week?

  13. 13

    Mongodb mongoose query get count of documents by reference

  14. 14

    How to get data and count in one query in mongoose

  15. 15

    how can i write as one query to get count in mysql

  16. 16

    How can I make a query in mongoose and node.js to count users depending on a value (similar to mysql distinct)?

  17. 17

    How can I get the average of the top N rows of a SQL query?

  18. 18

    How can I correctly implement an Hibernate SQL query starting from an SQL query that count the number of rows?

  19. 19

    Get total count of rows in pagination query

  20. 20

    get whole count of rows in a query with group condition

  21. 21

    Node - Mongoose 3.6 - Sort query with populated field

  22. 22

    Can't get rows for zero values in SQL query with COUNT and 3 JOINs (4 tables)

  23. 23

    How can I query with elemMatch in mongoose?

  24. 24

    How can I get a populated combobox to display its options in Swift?

  25. 25

    How can I get dropdown options populated from state?

  26. 26

    Can I have a LINQ query that counts total of rows filtered with a where on a property?

  27. 27

    Mongoose aggregate: How do I get all document that does not have a column populated?

  28. 28

    How I can count identical rows in sql

  29. 29

    How can I count matched rows individually?

HotTag

Archive