Sequelize:查找所有匹配包含(不区分大小写)

rya*_*e28 6 javascript sequelize.js

我想使用sequelize.js来查询具有包含约束的记录的模型.我怎么做?

这就是我现在所拥有的:

Assets
  .findAll({ limit: 10, where: ["asset_name like ?", '%' + request.body.query + '%'] })
  .then(function(assets){
    return response.json({
      msg: 'search results',
      assets: assets
    });
  })
  .catch(function(error){
    console.log(error);
  });
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

   { error: operator does not exist: character varying @> unknown
       at Connection.parseE (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:554:11)
       at Connection.parseMessage (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:381:17)
       at Socket.<anonymous> (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:117:22)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:188:7)
       at readableAddChunk (_stream_readable.js:176:18)
       at Socket.Readable.push (_stream_readable.js:134:10)
       at TCP.onread (net.js:548:20)
     name: 'error',
     length: 209,
     severity: 'ERROR',
     code: '42883',
     detail: undefined,
     hint: 'No operator matches the given name and argument type(s). You might need to add explicit type casts.',
     position: '246',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     schema: undefined,
     table: undefined,
     column: undefined,
     dataType: undefined,
     constraint: undefined,
     file: 'parse_oper.c',
     line: '722',
     routine: 'op_error',
     sql: 'SELECT "id", "asset_name", "asset_code", "asset_icon", "asset_background", "asset_add_view", "asset_add_script", "asset_add_id_regex", "date_created", "uniqueValue", "createdAt", "updatedAt" FROM "assets" AS "assets" WHERE "assets"."asset_name" @> \'%a%\' LIMIT 10;' },
  sql: 'SELECT "id", "asset_name", "asset_code", "asset_icon", "asset_background", "asset_add_view", "asset_add_script", "asset_add_id_regex", "date_created", "uniqueValue", "createdAt", "updatedAt" FROM "assets" AS "assets" WHERE "assets"."asset_name" @> \'%a%\' LIMIT 10;' }
Run Code Online (Sandbox Code Playgroud)

你如何在sequelize中使用包含查询?

Mar*_*žic 18

如果你在 Postgres 中使用 sequelize 更好用[Op.iLike]: `%${request.body.query}%` ,你可以忘记 sequelize 函数。

  • 不幸的是,似乎 [Op.iLike] 仅在使用 Postgres 时才有效 https://sequelize.org/master/manual/model-querying-basics.html (2认同)

pio*_*ias 12

Assets.findAll({
        limit: 10,
        where: {
            asset_name: {
                $like: '%' + request.body.query + '%'
            }
        }
}).then(function(assets){
    return response.json({
        msg: 'search results',
        assets: assets
    });
}).catch(function(error){
    console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

编辑

为了使它不区分大小写,您可以使用LOWERsql函数,但之前您还必须小写您的request.body.query值.Sequelize查询看起来就像那样

let lookupValue = request.body.query.toLowerCase();

Assets.findAll({
    limit: 10,
    where: {
        asset_name: sequelize.where(sequelize.fn('LOWER', sequelize.col('asset_name')), 'LIKE', '%' + lookupValue + '%')
    }
}).then(function(assets){
    return response.json({
        msg: 'message',
        assets: assets
    });
}).catch(function(error){
    console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

它的作用是asset_name从表中小写你的值,以及小写request.body.query值.在这种情况下,您比较两个较低的套管字符串.

为了更好地了解在这种情况下发生的事情,我建议你看看有关的sequelize文档sequelize.where(),sequelize.fn()以及sequelize.col().试图执行一些不寻常的查询,而不是简单的,当这些功能是非常有用的findAllfindOne.

sequelize这种情况下当然是你的Sequelize实例.

  • 如果您使用的是PG,则可以使用`[Op.iLike]:\`%$ {request.body.query}%\``并跳过下套管。 (3认同)

小智 5

// search case insensitive nodejs usnig sequelize


 const sequelize = require('sequelize');
    let search = "Ajay PRAJAPATI"; // what ever you right here
    userModel.findAll({
        where: {
            firstname: sequelize.where(sequelize.fn('LOWER', sequelize.col('firstname')), 'LIKE', '%' + search.toLowerCase() + '%')
        }
    })
Run Code Online (Sandbox Code Playgroud)