MongoDB 有一个重要的功能, 索引. 这是避免我们查询的时候每次都要从头全局遍历一下的必要方法.
有时候我们面临这样一个问题, 某个字段的值非常长, 那么索引这个字段会报错如下:
JavaScript
{
"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 即可.
根据官方文档, 我们一般是
JavaScript
db.some_collection.createIndex({"some_field":1})我们改成
JavaScript
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 还有很多特别的功能, 请查看官方文档.
Comments · 2