如何在mongo控制台中更新日期字段? [英] How to update date field in mongo console?
问题描述
例如,我想将所有记录更新为"2012-01-01"(时间":ISODate("2011-12-31T13:52:40Z")).
For example I want to update all records to '2012-01-01' ( "time" : ISODate("2011-12-31T13:52:40Z") ).
db.test.update( { time : '2012-01-01' }, false, true )
返回错误:
Assert failed : need an object
Error("Printing Stack Trace")@:0
()@shell/utils.js:35
("assert failed : need an object")@shell/utils.js:46
(false,"need an object")@shell/utils.js:54
([object Object],false,true)@shell/collection.js:189
@(shell):1
Wed Jan 11 17:52:35 uncaught exception: assert failed : need an object
推荐答案
您需要创建一个新的ISODate
对象,如下所示:
You need to create a new ISODate
object like this:
db.test.insert({"Time" : new ISODate("2012-01-10") });
对于更新和查询都是如此.请注意,您的查询语法不正确,应该是
This is true both for updates and for queries. Note that your query syntax is incorrect, it should be
db.test.update({ criteria }, { newObj }, upsert, multi);
例如,要更新所有对象,请考虑
For example, to update all objects, consider
db.test.update( {}, { $set : { "time" : new ISODate("2012-01-11T03:34:54Z") } }, true, true);
还请注意,这与
db.test.update( {}, { "time" : new ISODate("2012-01-11T03:34:54Z") }, true, false);
因为后者将替换对象,而不是将新字段添加到现有文档或更新现有字段.在此示例中,我将最后一个参数更改为false
,因为多重更新仅适用于$
运算符.
because the latter will replace the object, rather than add a new field to the existing document or updating the existing field. In this example, I changed the last parameter to false
, because multi updates only work with $
operators.
这篇关于如何在mongo控制台中更新日期字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!