MongoDB 有一个重要的功能, 索引. 这是避免我们查询的时候每次都要从头全局遍历一下的必要方法.

有时候我们面临这样一个问题, 某个字段的值非常长, 那么索引这个字段会报错如下:


{
  "ok" : 0,
  "errmsg" : "WiredTigerIndex::insert: key too large to index, 
    failing  1051 
      { : 
        \"000 new case ar diagnos and about 6000 women die....\" 
      }",
  "code" : 17280
}

处理方法也很简单, 我们使用 hashed index 即可.

根据官方文档, 我们一般是

db.some_collection.createIndex({"some_field":1})

我们改成

db.some_collection.createIndex({"some_field":"hashed"})

即可.

同时注意官方文档的警告. hashed index 在 index 浮点数的时候是有损的.

MongoDB hashed indexes truncate floating point numbers to 64-bit integers before hashing. For example, a hashed index would store the same value for a field that held a value of 2.3, 2.2, and 2.9. To prevent collisions, do not use a hashed index for floating point numbers that cannot be reliably converted to 64-bit integers (and then back to floating point). MongoDB hashed indexes do not support floating point values larger than 253.

另一种方法是创建 text index.

同理, 值从 1 改成 text 即可.

此外, text index 还有很多特别的功能, 请查看官方文档.

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

2 Comments

yoyo · November 23, 2016 at 19:59

Google Chrome 54.0.2840.98 Google Chrome 54.0.2840.98 Mac OS X  10.12.1 Mac OS X 10.12.1

哇,搜这个错误竟然弹出来你的 blog ~ 23333
text index 好像一个 collection 里最多设置一个 ∠(ᐛ」∠)_

    yu · November 24, 2016 at 10:56

    Google Chrome 54.0.2840.98 Google Chrome 54.0.2840.98 Mac OS X  10.12.1 Mac OS X 10.12.1

    @yoyo
    lol~

    一般用 hashed 应该就够用了.

Leave a Reply to yoyo Cancel reply

Your email address will not be published. Required fields are marked *