Waterline ORM (sails.js) "where or" in query
Asked Answered
F

2

16

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.

Froe answered 13/12, 2013 at 13:30 Comment(0)
C
29

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);
    });
},
Christoffer answered 13/12, 2013 at 17:49 Comment(2)
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)Froe
@user2659879, I think my edit should answer and resolve your issue.Christoffer
A
0
let userResult=await User.find().where({
score: { '>': parseInt(req.params.score)},
or: [{ status: 'user'},{status: 'admin'}]})
Anthracnose answered 16/4, 2019 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.