[英]How to increase a date field with one second value in a MongoDB 3.6?
MongoDB scripting is still pretty cryptic to me, and stuck with this problem on a 3.6 server lacking the fancier operators from 4.2. MongoDB 脚本对我来说仍然很神秘,并且在缺少 4.2 中更高级的操作员的 3.6 服务器上遇到了这个问题。
How to increase a date field with one second value in a MongoDB 3.6?如何在 MongoDB 3.6 中增加一秒值的日期字段?
For example, I'd like to do something like this:例如,我想做这样的事情:
db.getCollection("therecords").update(
{
"_id" : ObjectId("123etc")
},
{
$set: {
updatedAt : "$updatedAt" + 1 ;
}
}
);
…will set the value to the text $updatedAt1
which is cute. …将值设置为可爱的文本
$updatedAt1
。
Other cute errors are thrown when using updatedAt
without quotes or without the $ prefix.使用不带引号或不带 $ 前缀的
updatedAt
时会引发其他可爱的错误。
More generically, is there a way to reuse current values with update … $set
?更一般地说,有没有办法通过
update … $set
重用当前值?
I don't think it is possible to do this using single query in MongoDB v3.6, but:我认为在 MongoDB v3.6 中使用单个查询是不可能的,但是:
find()
specific documents find()
特定文件forEach()
found document, get the date and bump forEach()
找到文档,获取日期和凹凸save()
it back save()
它回来db.getCollection("therecords").find(
{
"_id" : ObjectId("123etc")
}
).forEach(function(doc) {
var d = new Date(doc.createdAt);
d.setSeconds(d.getSeconds() + 1);
doc.createdAt = d;
db.getCollection("therecords").save(doc); // Save each back
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.