javascript - 在 javascript 中增加日期,以更新 MongoDB

标签 javascript mongodb date

在 MongoDB 集合中,我有 3 个对象。我需要更新每个对象中的一个变量(日期类型)。主要任务是增加对象的日期。例如:所有对象都有相同的变量:

"Time1" : ISODate("2016-01-12T21:37:46.738Z")

我的问题是用当前日期更新第一个对象,我手动这样做:

$db.getCollection('my.data')({'_id':ObjectId("52e637fca92cf1ec6a73c1e8")}, {$currentDate: {Time1: true}})

下一步是将第二个对象的日期增加 1 天,我的意思是用明天的日期更新它。我无法通过 shell 执行此操作,因为 $inc 不适用于日期类型。 所以,我迷失了 javascript

我找到了如何使用 java 脚本获取它,但我不知道如何在一个脚本中收集所有内容。

var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);

感谢您的帮助。

最佳答案

您可以使用$set 运算符用于其他字段,一起为 $currentDate 操作符全部在更新对象内:

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);

var dayAfterTomorrow = new Date();
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate()+2);

db.getCollection("my.data").update(
    { "_id": ObjectId("52e637fca92cf1ec6a73c1e8") },
    { 
        "$currentDate": { "Time1": true },
        "$set": {
            "Time2": tomorrow,
            "Time3": dayAfterTomorrow
        }
    }
)

关于javascript - 在 javascript 中增加日期,以更新 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34744992/

相关文章:

javascript - 使用文件加载器将 Webpack 多入口点打包到一个文件中

javascript - React-virtualized WindowScroller 性能问题

javascript - 如何从 Mongo 中的数组中删除元素

PHP 按日期排序数组?

javascript - 日期和时间的时间戳 javascript

java.util.Date 在 JDK 5 和 JDK 6 中返回不同的日期

javascript - 有没有办法覆盖 vue 组件内的转换

javascript - 如何在包含事件的情况下使用其他参数

php - 使用 Li3 为 MongoDB 设置安全 => 'majority'

javascript - 函数 insertMany() 无序 : proper way to get both the errors and the result?