Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waterline ORM (sails.js) conditions with NOT Equal in query

How can I write a NOT Equal condition in Waterline?

This code:

Users
  .count()
  .where({
     id: { '!=': req.params.id},
     lastname: req.body.lastname
  })

Does nothing... (disk adapter in sails.js)

like image 979
Igor Avatar asked Dec 04 '13 15:12

Igor


2 Answers

First thing, it do nothing because looking into database is asynchronous. You have to make the chain longer and add exec or something from Q library like then.

User.count().where({
    id: { '!=': req.params.id },
    lastname: req.body.lastname
}).exec(function(err, num){
    console.log(num);
});

Now it returns 0. To make it return proper number instead of '!=' just write '!'.

like image 61
Paweł Wszoła Avatar answered Oct 16 '22 05:10

Paweł Wszoła


Adding this answer for anyone using Postgres. I was trying this and banging my head against a wall as it wasn't working and I couldn't figure out why. I searched all over and kept finding this answer.

If you are on postgres you want to use

id: { not: req.params.id }

After looking through Sailsjs docs and searching google long and hard I found this answer in the comments of the sails-postgresql module query.js. Hopefully this helps save someone in the same situation some time. Here is the full comment block that saved my sanity.

/**
 * Specifiy a `where` condition
 *
 * `Where` conditions may use key/value model attributes for simple query
 * look ups as well as more complex conditions.
 *
 * The following conditions are supported along with simple criteria:
 *
 *   Conditions:
 *     [And, Or, Like, Not]
 *
 *   Criteria Operators:
 *     [<, <=, >, >=, !]
 *
 *   Criteria Helpers:
 *     [lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual, not, like, contains, startsWith, endsWith]
 *
 * ####Example
 *
 *   where: {
 *     name: 'foo',
 *     age: {
 *       '>': 25
 *     },
 *     like: {
 *       name: '%foo%'
 *     },
 *     or: [
 *       { like: { foo: '%foo%' } },
 *       { like: { bar: '%bar%' } }
 *     ],
 *     name: [ 'foo', 'bar;, 'baz' ],
 *     age: {
 *       not: 40
 *     }
 *   }
 */
like image 42
Lenny Avatar answered Oct 16 '22 07:10

Lenny