以字符串格式更新mongo日期

我有一个收藏有2000万个文档的收藏集。 我想更新一个字段,该字段是表示此日期的字符串,格式为->“ timestamp”:“ 2019/10/03 05:24:45.000” 我想在符合特定条件的每一行中增加30天。

我已经完成了,但是表现却很差,我认为有更好的方法来做到这一点。

示例文档:

{ 
    "_id" : ObjectId("5d962843525def14360ca35b"),"sensor" : "sensor_87.202:h","timestamp" : "2019/10/03 05:24:45.000","value" : 5.0,"samplingFrequency" : NumberLong(15)
}

这是我的方法:

var bulk = db.Measure.initializeUnorderedBulkOp(),count = 0;
db.getcollection('Measure').aggregate([
    {$match:{sensor:'sensor_88.11:h',timestamp : {'$gt': '2019/09/02 11:40:00.000','$lt': '2019/10/02 11:41:00.000'}}},{$project:{sensor:1,timestamp:  {$dateToString: { format: '%Y/%m/%d %H:%M:%S.%L',date: {$add: [{$dateFromString: { dateString: '$timestamp'}},1000 * 3600 * 24 * 30]} } }}}
])
.forEach(function(x){ 
    bulk.find({'_id':x._id}).updateone({$set:{'timestamp':x.timestamp}});
    count++;
    if ( count % 1000 == 0 ) {
        bulk.execute();
        bulk = db.Measure.initializeUnorderedBulkOp();
    }
});
print(count);    
if ( count % 1000 !=0 ){
    bulk.execute();
}    

match和project阶段返回大约76k文档,这些文档必须在forEach中进行更新。 ¿有没有办法对所有匹配的文档进行无循环处理?

请注意,如果我将函数的内容替换为简单的print(“ test”);表现很好。所以我认为问题出在更新上。

shijy07 回答:以字符串格式更新mongo日期

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3159894.html

大家都在问