在查询中使用NOT Equal的水线ORM(sails.js)条件

Igo*_*gor 11 javascript orm node.js sails.js waterline

如何在水线中写出NOT Equal条件?

这段代码:

Users
  .count()
  .where({
     id: { '!=': req.params.id},
     lastname: req.body.lastname
  })
Run Code Online (Sandbox Code Playgroud)

什么都不做......(sails.js中的磁盘适配器)

Paw*_*oła 15

首先,它什么都不做,因为查看数据库是异步的.你必须让链更长,然后像Q库那样添加exec或者其他东西.

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

现在它返回0.要使它返回正确的数字而不是'!=',只需写'!'.


Len*_*nny 12

为使用Postgres的任何人添加此答案.我正在尝试这个并且撞在墙上,因为它不起作用,我无法弄清楚为什么.我搜索了一遍,并一直找到这个答案.

如果你在postgres你想使用

id: { not: req.params.id }
Run Code Online (Sandbox Code Playgroud)

在浏览了Sailsjs文档并长时间搜索谷歌之后,我在sails-postgresql模块query.js的评论中找到了这个答案.希望这有助于在某些时候拯救处于相同情况的人.这是完整的评论块,保存了我的理智.

/**
 * 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
 *     }
 *   }
 */
Run Code Online (Sandbox Code Playgroud)