node.js 序列化 OR 条件对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20695062/
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-09-02 16:32:02  来源:igfitidea点击:

Sequelize OR condition object

node.jssequelize.js

提问by Morio

By creating object like this

通过创建这样的对象

var condition=
{
  where:
  {
     LastName:"Doe",
     FirstName:["John","Jane"],
     Age:{
       gt:18
     }
  }    
}

and pass it in

并传入

Student.findAll(condition)
.success(function(students){

})

It could beautifully generate SQL like this

它可以漂亮地生成这样的 SQL

"SELECT * FROM Student WHERE LastName='Doe' AND FirstName in ("John","Jane") AND Age>18"

However, It is all 'AND' condition, how could I generate 'OR' condition by creating a condition object?

但是,这都是“AND”条件,如何通过创建条件对象来生成“OR”条件?

回答by Morio

Seems there is another format now

现在似乎有另一种格式

where: {
    LastName: "Doe",
    $or: [
        {
            FirstName: 
            {
                $eq: "John"
            }
        }, 
        {
            FirstName: 
            {
                $eq: "Jane"
            }
        }, 
        {
            Age: 
            {
                $gt: 18
            }
        }
    ]
}

Will generate

会产生

WHERE LastName='Doe' AND (FirstName = 'John' OR FirstName = 'Jane' OR Age > 18)

See the doc: http://docs.sequelizejs.com/en/latest/docs/querying/#where

请参阅文档:http: //docs.sequelizejs.com/en/latest/docs/querying/#where

回答by evanhadfield

Use Sequelize.or:

使用Sequelize.or

var condition = {
  where: Sequelize.and(
    { name: 'a project' },
    Sequelize.or(
      { id: [1,2,3] },
      { id: { lt: 10 } }
    )
  )
};

Reference(search for Sequelize.or)

参考(搜索Sequelize.or

Edit: Also, this has been modified and for the latest method see Morio's answer,

编辑:此外,这已被修改,有关最新方法,请参阅Morio 的回答

回答by CoredusK

String based operators will be deprecated in the future (You've probably seen the warning in console).

将来不推荐使用基于字符串的运算符(您可能已经在控制台中看到了警告)。

Getting this to work with symbolic operators was quite confusing for me, and I've updated the docswith two examples.

让它与符号运算符一起工作对我来说很困惑,我已经用两个例子更新了文档

Post.findAll({
  where: {
    [Op.or]: [{authorId: 12}, {authorId: 13}]
  }
});
// SELECT * FROM post WHERE authorId = 12 OR authorId = 13;

Post.findAll({
  where: {
    authorId: {
      [Op.or]: [12, 13]
    }
  }
});
// SELECT * FROM post WHERE authorId = 12 OR authorId = 13;

回答by Evan Siroky

See the docs about querying.

请参阅有关查询文档

It would be:

这将是:

$or: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)

回答by inmyth

For Sequelize 4

对于续集 4

Query

询问

SELECT * FROM Student WHERE LastName='Doe' 
AND (FirstName = "John" or FirstName = "Jane") AND Age BETWEEN 18 AND 24 

Syntax with Operators

运算符的语法

const Op = require('Sequelize').Op;

var r = await to (Student.findAll(
{
  where: {
    LastName: "Doe",
    FirstName: {
      [Op.or]: ["John", "Jane"]
    },
    Age: {
      // [Op.gt]: 18
      [Op.between]: [18, 24]
    }
  }
}
));

Notes

笔记

  • For better securitySequelize recommends dropping alias operators $(e.g $and, $or...)
  • Unless you have {freezeTableName: true}set in the table model then Sequelize will query against the plural form of its name ( Student -> Students)
  • 为了更好的安全性,Sequelize 建议删除别名操作符$(例如$and$or...)
  • 除非您{freezeTableName: true}在表模型中进行了设置,否则Sequelize 将查询其名称的复数形式( Student -> Students

回答by Muhammad Fari

In Sequelize version 5 you might also can use this way (full use Operator Sequelize) :

在 Sequelize 版本 5 中,您也可以使用这种方式(完全使用 Operator Sequelize):

var condition = 
{ 
  [Op.or]: [ 
   { 
     LastName: {
      [Op.eq]: "Doe"
      },
    },
   { 
     FirstName: {
      [Op.or]: ["John", "Jane"]
      }
   },
   {
      Age:{
        [Op.gt]: 18
      }
    }
 ]
}

And then, you must include this :

然后,你必须包括这个:

const Op = require('Sequelize').Op

and pass it in :

并将其传入:

Student.findAll(condition)
.success(function(students){ 
//
})

It could beautifully generate SQL like this :

它可以漂亮地生成这样的 SQL:

"SELECT * FROM Student WHERE LastName='Doe' OR FirstName in ("John","Jane") OR Age>18"