You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SailsJS 1.2.3对接MongoDB查询大小写敏感设置问题求助

Fixing Case-Insensitive Queries in Sails.js 1.2.3 with MongoDB

Hey there, I’ve tackled this exact problem after upgrading from Sails 0.12 to 1.x—those sudden case-sensitive queries can wreak havoc on existing functionality, so let’s get this sorted quickly.

First, let’s clarify why your old wlNext: { caseSensitive: false } config stopped working: Sails 1.x fully retired the wlNext preview flags when it launched the stable Waterline v0.13+ ORM. That setting was specific to the 0.12 beta era, so it’s no longer recognized.

Here are the three most reliable solutions tailored to your stack (Sails 1.2.3, sails-mongo 1.1.0):


1. Global Case-Insensitive Queries (All Models/Datastores)

Set a default collation for your MongoDB datastore to apply case-insensitive matching across every query. This is the easiest fix for universal behavior.

Edit config/datastores.js and add the collation option to your MongoDB config:

module.exports.datastores = {
  default: {
    adapter: 'sails-mongo',
    url: 'mongodb://localhost:27017/your-database-name',
    // Add this collation block
    collation: {
      locale: 'en', // Match your data's language (e.g., 'zh' for Chinese)
      strength: 2   // Critical: Strength 2 ignores case; 1 ignores case + accents, 3 is strict
    }
  }
};

Restart your Sails app, and all queries will now ignore case by default.


2. Per-Model or Per-Query Case Insensitivity

If you only need case insensitivity for specific models or queries (instead of globally), use these approaches:

Per-Model Defaults

Add a custom collation to your model’s configuration to enforce it for all queries on that model. In your model file (e.g., api/models/User.js):

module.exports = {
  attributes: {
    email: { type: 'string', required: true },
    username: { type: 'string', required: true }
  },
  // Apply collation to all queries for this model
  customToMongoSchema: function(schema) {
    // Optional: Add a case-insensitive index for better query performance
    schema.index({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
    schema.index({ username: 1 }, { collation: { locale: 'en', strength: 2 } });
    return schema;
  },
  // Override find/findOne/etc. to use collation
  find: function(criteria) {
    return this.baseFind(criteria).collation({ locale: 'en', strength: 2 });
  }
};

Per-Query Override

For one-off queries, explicitly specify the collation:

// Find a user by email, ignoring case
const user = await User.find({ email: 'USER@EXAMPLE.COM' })
  .collation({ locale: 'en', strength: 2 });

3. Case-Insensitive Indexes (Performance Boost)

If you have large datasets, creating case-insensitive indexes will make your queries much faster (avoids full-collection scans). You can create these either via your model or directly in MongoDB:

Via Model (Automatic Migration)

Use the customToMongoSchema method shown in the per-model section above to define indexes with collation.

Directly in MongoDB Shell

Run this command for each field you want to index:

db.yourCollection.createIndex({ email: 1 }, { collation: { locale: 'en', strength: 2 } });

Replace yourCollection with your actual MongoDB collection name (usually matches your model name, lowercase pluralized).


Key Notes

  • Collation Locale: Make sure the locale value matches your data’s language (e.g., 'fr' for French, 'es' for Spanish). 'en' works for most English-language apps.
  • sails-mongo Version: Your current version (1.1.0) fully supports collation, so no need to upgrade the adapter.
  • Testing: After setting up, verify with queries that use mixed case (e.g., searching for 'JohnDoe' should return 'johndoe' and vice versa).

Let me know if you hit any snags getting this configured—I’m happy to help troubleshoot further!

内容的提问来源于stack exchange,提问作者Shashan Sooriyahetti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:32:46