You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何查看或修改MongoDB集合的collation选项及创建时设置的集合选项?

Got it, let's break down your questions step by step—since you're working with MongoDB's collation and collection options, I'll walk you through exactly how to view, modify, and verify these settings, especially that collation strength change you need.

查看集合已配置的选项

First off, to check all settings (including collation) for an existing collection, use the getOptions() method on the collection object—this is a built-in shell function you might have missed earlier:

db.words.getOptions()

When you run this, the returned document will include a collation field if you set one during collection creation, like this:

{
  "collation": {
    "locale": "en",
    "strength": 2,
    "caseLevel": false,
    // Other collation properties (if applicable)
  },
  // Additional collection options like capped, validator, etc.
}

If no explicit collation was set during creation, this field won't appear, meaning the collection uses the database's default collation (or MongoDB's default simple locale).

修改集合的collation(Important: No direct edit allowed!)

You mentioned not finding a direct way to modify collation—and that's intentional: MongoDB doesn't support directly altering the collation of an existing collection. Collation is a core collection property that impacts sorting rules, index behavior, and data consistency; changing it directly would break existing indexes and risk data integrity.

Instead, you'll use a "copy-and-replace" workflow to update the collation:

  1. Create a new collection with your target collation
    Assuming your original collection uses the en locale and you want to set strength to 3, create a temporary collection with the desired settings:

    db.createCollection("words_temp", {
      collation: {
        locale: "en",
        strength: 3,
        // Keep other original collation properties (like caseLevel) if needed
        caseLevel: false
      }
    })
    
  2. Migrate data from the original collection to the new one
    For small datasets, use a simple loop:

    db.words.find().forEach(doc => db.words_temp.insertOne(doc))
    

    For larger datasets, use the more efficient $out aggregation (this will overwrite the target collection, so ensure it's empty first):

    db.words.aggregate([{ $out: "words_temp" }])
    
  3. Replace the original collection

    • Rename the original collection as a backup first:
      db.words.renameCollection("words_backup")
      
    • Rename the temporary collection to the original name:
      db.words_temp.renameCollection("words")
      
  4. Delete the backup (optional, once verified)
    After confirming data and settings are correct, you can drop the backup:

    db.words_backup.drop()
    
Verify the updated collation settings

Once the replacement is done, re-run getOptions() to confirm the change:

db.words.getOptions()

You’ll see the collation.strength field now shows 3.

To test the actual behavior, insert some test documents and sort them using the collection's default collation:

db.words.insertMany([
  { text: "Apple" },
  { text: "apple" },
  { text: "Äpple" }
])

// Sort using the collection's new default collation
db.words.find().sort({ text: 1 })

With strength 3, the sort will distinguish between uppercase and lowercase but ignore accents—you’ll see results ordered like apple, Apple, Äpple (exact order depends on the locale rules, but the key is case differentiation, which wouldn’t happen with strength 2).

Bonus: Use a different collation temporarily

If you only need to use a specific collation for a single query or sort (without modifying the collection), just add the collation parameter directly to the operation:

// Query with temporary strength 3 collation
db.words.find({ text: "apple" }).collation({ locale: "en", strength: 3 })

// Sort with temporary strength 3 collation
db.words.find().sort({ text: 1 }).collation({ locale: "en", strength: 3 })

This won’t change the collection’s default settings—it only applies to that one operation.

内容的提问来源于stack exchange,提问作者Andrew Nessin

火山引擎 最新活动