15

I would like to know how to add "OR" condition in waterline query. Should look like:

User.find().where({

    score: { '>': req.params.score},
    status: 'user'
    OR
    status: 'admin'


}).exec(function(err, data){
    ...
});

So we have 2 conditions:

1) Score > specific number

And 2) status = user

OR

1) Status = admin.

TheSharpieOne
  • 25,646
  • 9
  • 66
  • 78
Igor
  • 825
  • 2
  • 7
  • 18

2 Answers2

28

There was an issue with the development database used by sails (waterline-criteria). The issue was the way strings and integers were handled in sails-disk. In the query criteria below, theScore, was being treated as a string. This has been resolved, so you just need to update sails-disk. You can do this by using npm install sails-disk --force --save. After that the example below should work fine.

You can try this (Updated):

    foo: function(req, res, next) {

    var theScore = req.param('id') || 0;

    User.find().where({

        or: [{

        score: {
            '>': parseInt(theScore),
        },

        status: 'user'
        },

      {  status: 'admin'}]

    }).exec(function(err, data) {
        if (err) return next(err);
        res.json(data);
    });
},
JohnGalt
  • 2,851
  • 2
  • 21
  • 29
  • Thanks you, but not exactly what I need. So for admin I don't need to check scrope. (So for admin we can have any score, but for user more than param) – Igor Dec 16 '13 at 07:13
  • 1
    @user2659879, I think my edit should answer and resolve your issue. – JohnGalt Dec 17 '13 at 20:20
0
let userResult=await User.find().where({
score: { '>': parseInt(req.params.score)},
or: [{ status: 'user'},{status: 'admin'}]})
Garach Ankur
  • 48
  • 1
  • 5