修复:MongoDB Robomongo:db.data.find(…).collation 不是函数?
排序于 MongoDB 3.4 版引入。也许,你在前一个版本中实现了排序。
对于我们的示例,我们使用 MongoDB 4.0.5 版本。以下是查询系统当前版本的查询 −
> db.version()
这将产生以下输出 −
4.0.5
我们首先创建一个带文档的集合 −
> 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")
}以下是要借助 find() 方法显示集合的所有文档的查询 −
> db.collationExample.find().pretty();
这将产生以下输出 −
{ "_id" : ObjectId("5e038a3cf5e889d7a51994f5"), "Value" : "x" }
{ "_id" : ObjectId("5e038a48f5e889d7a51994f6"), "Value" : "X" }
{ "_id" : ObjectId("5e038a49f5e889d7a51994f7"), "Value" : "Y" }
{ "_id" : ObjectId("5e038a49f5e889d7a51994f8"), "Value" : "a" }
{ "_id" : ObjectId("5e038a4bf5e889d7a51994f9"), "Value" : "á" }以下是使用 COLLATION() 的查询 −
> db.collationExample.find({ Value: "a" } ).collation( { locale: "en", strength: 1 } );这将产生以下输出 −
{ "_id" : ObjectId("5e038a49f5e889d7a51994f8"), "Value" : "a" }
{ "_id" : ObjectId("5e038a4bf5e889d7a51994f9"), "Value" : "á" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP