我想对数组records
进行排序,并删除仅包含空格的" "
," "
项。
如何通过聚合获得它。
这是我粗略的想法,但没有工作
pipeline = [
{'$unwind': '$records'}
{ '$sort':
"records.date": 1
}
]
db[source_collection].runCommand('aggregate',
pipeline: pipeline
allowDiskUse: true)
示例文档格式
{
"_id": "0005dc158e68b0a2e4f2a8d96ca733f1",
"records": [
{
"date": new Date("2012-12-04T08:00:00+0800"),
"items": [
" ",
"4659 ",
"463 "
]
},
{
"date": new Date("2012-12-01T08:00:00+0800"),
"items": [
" ",
"4658 "
]
},
{
"date": new Date("2012-10-18T08:00:00+0800"),
"items": [
" ",
"78900 "
]
},
{
"date": new Date("2012-08-07T08:00:00+0800"),
"items": [
" ",
"5230 "
]
}
],
"gender": "F",
"birthday": new Date("1990-01-01T08:00:00+0800"),
"birthday_type": "D"
}
答案 0 :(得分:1)
对于使用records
对date
数组进行排序,请检查以下聚合查询:
db.collectionName.aggregate({"$unwind":"$records"},{"$sort":{"records.date":1}}).pretty()
要更改items
数组值,您需要使用mongo Bulk编写编程代码。我编写以下脚本,更新每个items
数组值检查,如下所示:
var bulk = db.collectionName.initializeOrderedBulkOp();
var counter = 0;
db.collectionName.find({
"records": {
"$exists": true
}
}).forEach(function(data) {
for(var ii = 0; ii < data.records.length; ii++) {
var items = data.records[ii].items;
var arr = [];
for(var j = 0; j < items.length; j++) {
arr[j] = data.records[ii].items[j].trim()
}
var updoc = {
"$set": {}
};
var updateKey = "records." + ii + ".items";
updoc["$set"][updateKey] = arr;
// queue the update
bulk.find({
"_id": data._id
}).update(updoc);
counter++;
// Drain and re-initialize every 1000 update statements
if(counter % 1000 == 0) {
bulk.execute();
bulk = db.collectionName.initializeOrderedBulkOp();
}
}
})
// Add the rest in the queue
if(counter % 1000 != 0) bulk.execute();