0

I'm working to count total rows, in case the column name is nama_materi with condition = "HTML".

Here's my code:

getTotalListHTML: (req, res, next) => {
    modelsListMateris.find({nama_materi:'HTML'}, (err, result) => {
      if (result) res.send(result);
      else res.send(err);
    })
  },

I expect to add method count() after find statement, but it's not working. Thank you for helping me!

2 Answers 2

1

In Mongoose to get Count from a model

modelsListMateris.count({nama_materi:'HTML'}, (err, count) => {
        console.log( "Number of docs: ", count );
        if (count) res.send(count);
        else res.send(err);
    });

You can also use chaining functions like

modelsListMateris.find({nama_materi:'HTML'}).count(function(err, count){
    console.log("Number of docs: ", count );
    if (count) res.send(count);
    else res.send(err);
});

If you want rows with count you can use plugin mongoose-paginate and use it like this

 modelsListMateris.paginate({nama_materi:'HTML'}, {limit: 10, offset: 0})
Sign up to request clarification or add additional context in comments.

1 Comment

your 2nd code is working but got an error 500 and still can't to get count from a model..
0

I assume by rows you mean "documents".

Use the following mongo method:-

db.modelsListMateris.countDocuments( {nama_materi:'HTML'}, {} )

2 Comments

can't to use countDocuments method
the simply use count() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.