连续选择*,其中属性不是x

问题描述 投票:0回答:5

查看您可以使用的docs

model.findAll({where: {attribute: x}})
。但是,我想选择所有不是 x 的属性。我正在研究正则表达式,但这似乎不是最佳解决方案。

最好的方法是什么?

mysql node.js orm sequelize.js
5个回答
73
投票

更新了方法,适用于现代 Sequelize:

model.findAll({
  where: {
    someAttribute: {
      [sequelize.Op.not]: 'some value'
    }
  }
});

39
投票

试试这个

model.findAll({where: {attribute: { $not: 'x'}}})

Sequelize 实际上提供了很多运算符供您获取过滤数据。

您可以参考这里

的链接

7
投票

使用[Op.ne]操作符。 ne 是不等于运算符。记得从 'sequelize' 导入 { Op };

model.findAll({
      where: {
        attribute: {[Op.ne]:'x'} /*eg status: {[Op.ne]:'pending'}*/
      }
});

6
投票

请注意

$not:'x
已弃用,并且在最新版本中不起作用。

现在我们可以使用

[Op.gt
来代替:

model.findAll({
  where: {
    attribute: {[Op.not]:'x'}
  }
});

请查看更多详情这里


0
投票

要使用 sql 运算符,您也可以直接

import {Op} from 'sequelize'
,请查看模型查询下的运算符文档

import {Op} from 'sequelize';
// or const { Op } = require("sequelize");

  // ...

  await ChatUsers.update(
      {unread: true},
      {where: {
        chatId,
        userId: {[Op.not]: req.currentUser.id},
      }},
  );
© www.soinside.com 2019 - 2024. All rights reserved.