Lỗi collation is not a function xảy ra khi dùng collation trên MongoDB < 3.4. Collation được giới thiệu từ phiên bản 3.4. Kiểm tra phiên bản:
> db.version() 4.0.5
Tạo collection với collation index:
> db.collationExample.createIndex({Value: 1}, {collation: {locale: "en", strength: 1}});
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
> db.collationExample.insertOne({'Value':'x'});
{ "acknowledged" : true, "insertedId" : ObjectId("5e038a3cf5e889d7a51994f5") }
> db.collationExample.insertOne({'Value':'X'});
{ "acknowledged" : true, "insertedId" : ObjectId("5e038a48f5e889d7a51994f6") }
> db.collationExample.insertOne({'Value':'Y'});
{ "acknowledged" : true, "insertedId" : ObjectId("5e038a49f5e889d7a51994f7") }
> db.collationExample.insertOne({'Value':'a'});
{ "acknowledged" : true, "insertedId" : ObjectId("5e038a49f5e889d7a51994f8") }
> db.collationExample.insertOne({'Value':'á'});
{ "acknowledged" : true, "insertedId" : ObjectId("5e038a4bf5e889d7a51994f9") }
Hiển thị tài liệu:
> db.collationExample.find().pretty();
Sử dụng collation trong find (strength: 1 = so sánh base character, bỏ qua dấu và hoa/thường):
> db.collationExample.find({ Value: "a" }).collation( { locale: "en", strength: 1 } );Kết quả - khớp "a" và "á":
Giải pháp: Nâng cấp MongoDB lên >= 3.4. Robomongo/Studio 3T cũ có thể không hỗ trợ collation method trên cursor.