查询中包含NOT Equal的水线ORM(sails.js)条件

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

本文关键字:ORM sails js 条件 包含 NOT Equal 查询      更新时间:2024-02-28

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

此代码:

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

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

首先,它什么也不做,因为查看数据库是异步的。你必须使链更长,并添加exec或Q库中的类似内容。

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

现在它返回0。使其返回正确的数字而不是"!="只需写"!"。

为任何使用Postgres的人添加这个答案。我试着这样做,头撞在墙上,因为它不起作用,我不明白为什么。我到处找,一直找到这个答案。

如果你在postgres上,你想使用

id: { not: req.params.id }

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

我没有直接使用count,我使用了长度来了解count。

let user = User.find({
   where: {
    id: { '!=': req.params.id },
    lastname: req.body.lastname
  }
});
let count = user.length;