我想了解如何使用MongoDB
执行此操作我的文件名称为" file1"," file2"," file22"," file11" (名称可以是任何东西,没有特定的模式) 我运行查询以获取按名称排序的所有文档,结果不符合预期。
> db.mydata.find().sort({"name":1});
{ "_id" : ObjectId("571e5a787e88d30b20b7857c"), "name" : "file1" }
{ "_id" : ObjectId("571e5a8c7e88d30b20b7857d"), "name" : "file11" }
{ "_id" : ObjectId("571e5a977e88d30b20b7857f"), "name" : "file2" }
{ "_id" : ObjectId("571e5a937e88d30b20b7857e"), "name" : "file22" }
预期的是(字母/自然顺序)
{ "_id" : ObjectId("571e5a787e88d30b20b7857c"), "name" : "file1" }
{ "_id" : ObjectId("571e5a977e88d30b20b7857f"), "name" : "file2" }
{ "_id" : ObjectId("571e5a8c7e88d30b20b7857d"), "name" : "file11" }
{ "_id" : ObjectId("571e5a937e88d30b20b7857e"), "name" : "file22" }
根据我的发现,还有其他方法可以排序,例如使用aggregate
+ $project
和$meta: "textScore"
,但到目前为止我还没有成功。
答案 0 :(得分:8)
MongoDB没有提供开箱即用的方法,但您仍有两个选择:
第一种是使用Array.prototype.sort
方法对数组结果进行排序的客户端处理。
db.mydata.find().toArray().sort((a, b) => {
var x = Number(a.name.match(/\d+/g)[0]);
var y = Number(b.name.match(/\d+/g)[0]);
return x === y ? 0 :( x < y ? -1 : 1 );
})
我建议您做的第二件事是使用一个额外字段来规范化您的文档,该字段将“名称”中的数字保存为整数,并使用该值对文档进行排序。这意味着,您需要更新文档才能添加该字段,最好的方法是使用$set
更新运算符和"bulk operations"以获得最高效率。话虽如此,从MongoDB服务器版本3.2开始,您需要使用collection.bulkWrite
方法来实现此目的。
var requests = [];
db.mydata.find({}, { "name": 1 } ).forEach(doc => {
var fileId = Number(doc.name.match(/\d+/g)[0]); // return number from "name" value
requests.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": { "$set": { "fileId": fileId } }
}
});
// Execute per 1000 operations and re-init the requests queue
if( requests.length === 1000 )
db.mydata.bulkWrite(requests);
})
// Clean up queues
if (requests.length > 0)
db.mydata.bulkWrite(requests);
从 MongoDB服务器版本2.6 ,您需要使用现已弃用的Bulk
API。
var bulk = db.mydata.initializeUnorderedBulkOp();
var count = 0;
db.collection.find({}, { "name": 1 }).forEach(function(doc) {
var fileId = Number(doc.name.match(/\d+/g)[0]);
bulk.find({"_id": doc._id}).updateOne({
"$set": { "fileId": fileId }
});
count++;
if (count % 1000 === 0) {
bulk.execute();
bulk = db.mydata.initializeUnorderedBulkOp();
}
})
if (count > 0)
bulk.execute();
从 MongoDB服务器版本2.4 开始,您需要采用不同的方法。
db.collection.find({}, { "name": 1 }).forEach(function(doc) {
var fileId = Number(doc.name.match(/\d+/g)[0]);
db.collection.update(
{ "_id": doc._id },
{"$set": { "fileId": fileId } }
);
})
执行此操作后,您的文档现在如下所示:
{ "_id" : ObjectId("571e5a787e88d30b20b7857c"), "name" : "file1", "fileId" : 1 }
{ "_id" : ObjectId("571e5a8c7e88d30b20b7857d"), "name" : "file11", "fileId" : 11 }
{ "_id" : ObjectId("571e5a977e88d30b20b7857f"), "name" : "file2", "fileId" : 2 }
{ "_id" : ObjectId("571e5a937e88d30b20b7857e"), "name" : "file22", "fileId" : 22 }
现在,您可以使用.sort
方法轻松对文档进行排序。
db.mydata.find({}, { "name": 1 } ).sort( { "fileId": 1 } )
产生以下结果:
{ "_id" : ObjectId("571e5a787e88d30b20b7857c"), "name" : "file1" }
{ "_id" : ObjectId("571e5a977e88d30b20b7857f"), "name" : "file2" }
{ "_id" : ObjectId("571e5a8c7e88d30b20b7857d"), "name" : "file11" }
{ "_id" : ObjectId("571e5a937e88d30b20b7857e"), "name" : "file22" }