Sequelize OR condition object

后端 未结 6 1733
-上瘾入骨i
-上瘾入骨i 2020-12-08 03:38

By creating object like this

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


        
相关标签:
6条回答
  • 2020-12-08 04:07

    In Sequelize version 5 you might also can use this way (full use 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 :

    "SELECT * FROM Student WHERE LastName='Doe' OR FirstName in ("John","Jane") OR Age>18"
    
    0 讨论(0)
  • 2020-12-08 04:13

    Use Sequelize.or:

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

    Reference (search for Sequelize.or)

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

    0 讨论(0)
  • 2020-12-08 04:14

    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 docs with 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;
    
    0 讨论(0)
  • 2020-12-08 04:14

    For Sequelize 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 security Sequelize 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 )
    0 讨论(0)
  • 2020-12-08 04:20

    See the docs about querying.

    It would be:

    $or: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)
    
    0 讨论(0)
  • 2020-12-08 04:24

    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

    0 讨论(0)
提交回复
热议问题