Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js count where

I'm trying to count a subset of a collection, where the subset determined by an attribute. The code is:

User.count()
    .where({attribute:{'<':10}})
    .exec(function(err, users){
        callback(err, users);
    }); 

User.count by itself will return the total collection count, but putting in any WHERE clauses seems to consistently return 0. Does the count method not support WHERE clauses?

NB I'm using the Mongodb adapter both for this collection and by default. The 'attribute' exists on all models and is populated with numerical data (above and below 10).

like image 337
mrmagooey Avatar asked Dec 04 '22 07:12

mrmagooey


1 Answers

.count() now supports criteria, too. Take a look at the docs: https://github.com/balderdashy/sails-docs/blob/master/reference/waterline/models/count.md

In a nutshell:

User.count({name:'Flynn'}).exec(function countCB(err, found){
  console.log('There are '+found+' users called "Flynn".');
});
like image 117
Wesley Overdijk Avatar answered Dec 28 '22 18:12

Wesley Overdijk