Sails.js upgrade to v1 reverse case sensitive queries

試著忘記壹切 提交于 2019-12-08 01:32:08

问题


After upgrading to sails v1 all the requests in the controllers became case sensitive.

Although this is expected, commented here: https://sailsjs.com/documentation/concepts/models-and-orm/models#?case-sensitivity, I would like to have case insensitive behavior.

In my queries this is a problem and I am not able to figure out a way to make it NON case sensitive again. I am using MongoDB in production.

Any kind of help or suggestion would be much appreciated.


回答1:


For MongoDB we need to do a native mongo query to get case-insensitive:

const collection = Pet.getDatastore().manager.collection(Pet.tableName);
const res = await collection.find({ name: { $regex: /blue/, $options: 'i' } });
const dataWithObjectIds = await res.toArray();
const dataWithIds = JSON.parse(JSON.stringify(rawDataArr).replace(/"_id"/g, '"id"'));

See here for more on native mongo query - https://stackoverflow.com/a/54830620/1828637




回答2:


As the sails docs you linked specify, you should do this in the database:

Most databases are case sensitive by default but in the rare cases where they aren't and you would like to change that behavior you must modify the database to do so.

Since you're using MongoDB, that means creating a case insensitive index:

db.collection.createIndex({ key: 1 }, {
    collation: {
        locale: 'en',
        strength: 1
      }
})


来源:https://stackoverflow.com/questions/49849862/sails-js-upgrade-to-v1-reverse-case-sensitive-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!