mrmagooey
mrmagooey

Reputation: 4982

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).

Upvotes: 4

Views: 5061

Answers (2)

Wesley Overdijk
Wesley Overdijk

Reputation: 1196

.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".');
});

Upvotes: 16

zieglar
zieglar

Reputation: 830

You can use User.countBy( criteria , [callback] ) in sails v0.10.0-rc4 :) Model Methods countBy

Upvotes: 0

Related Questions