How to update date field in mongo console?

前端 未结 3 462
小蘑菇
小蘑菇 2021-02-02 07:00

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,          


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 07:50

    If you need to convert an existing date field (imported from MySQL format 'yyyy-mm-dd' f.e.) to ISODate you can loop through the documents this way:

    /usr/bin/mongo yourdbname --eval "db.yourcollectionname.find().forEach(function(doc){doc.yourdatefield = new ISODate(doc.yourdatefield);db.yourcollectionname.save(doc)});"
    

提交回复
热议问题