11

如何在 Waterline 中编写 NOT Equal 条件?

这段代码:

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

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

4

3 回答 3

16

首先,它什么都不做,因为查看数据库是异步的。您必须使链条更长,然后像这样从 Q 库中添加 exec 或其他内容。

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

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

于 2013-12-05T13:14:56.360 回答
12

为使用 Postgres 的任何人添加此答案。我正在尝试这个并将我的头撞到墙上,因为它不起作用,我不知道为什么。我到处搜索并不断找到这个答案。

如果你在 postgres 你想使用

id: { not: req.params.id }

在浏览了 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
 *     }
 *   }
 */
于 2014-03-24T03:26:46.227 回答
0

我没有直接用count,我用length知道count。

let user = User.find({
   where: {
    id: { '!=': req.params.id },
    lastname: req.body.lastname
  }
});
let count = user.length;
于 2021-01-26T11:51:39.743 回答