gpt4 book ai didi

MongoDB:如何为集合中所有值的特定字段运行函数?

转载 作者:可可西里 更新时间:2023-11-01 09:59:25 25 4
gpt4 key购买 nike

我想从 mongo shell 中执行此操作。基本上,我想更改时间在当前数据库中的存储方式。

现在我的“时间”字段正在存储一个看起来像“Thu Oct 11 2012 15:27:58 GMT-0500 (CDT)”的字符串,但我想运行 Date.parse('Thu Oct 11 2012 15:27:58 GMT-0500 (CDT)') 以便改为存储 unix 时间戳。

我想对所有当前条目全面执行此操作,因为我将来只会使用 unix 时间戳。

谢谢

最佳答案

怎么样:

var c = db.collection.find();
while (c.hasNext()) {
object = c.next();
time = Date.parse(object.time);
db.collection.update({_id: object._id}, {$set: {'time': time}});
}

在执行它之前,我有以下内容:

db.times.find()
{ "_id" : ObjectId("50773daa77f428a7e4cd226b"), "time" : "Thu Oct 11 2012 15:27:58 GMT-0500 (CDT)" }

执行后是这样的:

db.times.find()
{ "_id" : ObjectId("50773daa77f428a7e4cd226b"), "time" : 1349987278000 }

希望这对您有所帮助!

关于MongoDB:如何为集合中所有值的特定字段运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com