javascript 查询中 NOT Equal 的 Waterline ORM (sails.js) 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20378997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:21:40  来源:igfitidea点击:

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

javascriptnode.jsormsails.jswaterline

提问by Igor

How can I write a NOT Equal condition in Waterline?

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

This code:

这段代码:

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

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

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

回答by Pawe? Wszo?a

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.

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

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 '!'.

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

回答by Lenny

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.

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

If you are on postgres you want to use

如果你在 postgres 上你想使用

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.

在浏览了 Sailsjs 文档并在 google 上搜索了很长时间后,我在 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
 *     }
 *   }
 */