Computer >> Máy Tính >  >> Lập trình >> MongoDB

Khắc phục:MongoDB Robomongo:db.data.find (…) .collation không phải là một hàm?

Đối chiếu được giới thiệu trong phiên bản MongoDB 3.4. Có thể, bạn đã triển khai đối chiếu trong phiên bản trước.

Ví dụ của chúng tôi, chúng tôi đang sử dụng MongoDB phiên bản 4.0.5. Sau đây là truy vấn để kiểm tra phiên bản hiện tại trên hệ thống -

> db.version()

Điều này sẽ tạo ra kết quả sau -

4.0.5

Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> 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")
}

Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -

> db.collationExample.find().pretty();

Điều này sẽ tạo ra kết quả sau -

{ "_id" : ObjectId("5e038a3cf5e889d7a51994f5"), "Value" : "x" }
{ "_id" : ObjectId("5e038a48f5e889d7a51994f6"), "Value" : "X" }
{ "_id" : ObjectId("5e038a49f5e889d7a51994f7"), "Value" : "Y" }
{ "_id" : ObjectId("5e038a49f5e889d7a51994f8"), "Value" : "a" }
{ "_id" : ObjectId("5e038a4bf5e889d7a51994f9"), "Value" : "á" }

Đây là truy vấn sử dụng COLLATION () -

> db.collationExample.find({ Value: "a" } ).collation( { locale: "en", strength: 1 } );

Điều này sẽ tạo ra kết quả sau -

{ "_id" : ObjectId("5e038a49f5e889d7a51994f8"), "Value" : "a" }
{ "_id" : ObjectId("5e038a4bf5e889d7a51994f9"), "Value" : "á" }