Mongoid can not query non-latin attributes

56 Views Asked by At

My Mongoid document has two attributes: :en_name and ru_name. I have created one model:

MyModel.create(en_name: 'sport', ru_name: 'спорт')

Then I query it:

MyModel.where(en_name: 'sport').first

It returns me my model.

When I try to query this:

MyModel.where(ru_name: 'спорт').first

It returns me nil

How to make Mongoid able to query attributes which are non-latin?

1

There are 1 best solutions below

3
On

Mongodb uses UTF-8. However, you may experience problems if the server is running on Windows because Windows uses CP1251. Use Robomongo (cross-platform graphical client) for that would make sure that the data has been written to the database in the correct encoding. BSON can be encoded only in UTF-8. If the data is not displayed correctly, you probably are not converting your data to UTF-8 before uploading it to mongodb.

Check encoding encoding_name = str.encoding.name
Convert encoding utf_str = Iconv.conv('windows-1251', 'utf-8', str)