问题
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