Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to perform a "dry run" of an update operation?

Tags:

mongodb

I am in the process of changing the schema for one of my MongoDB collections. (I had been storing dates as strings, and now my application stores them as ISODates; I need to go back and change all of the old records to use ISODates as well.) I think I know how to do this using an update, but since this operation will affect tens of thousands of records I'm hesitant to issue an operation that I'm not 100% sure will work. Is there any way to do a "dry run" of an update that will show me, for a small number of records, the original record and how it would be changed?


Edit: I ended up using the approach of adding a new field to each record, and then (after verifying that the data was right) renaming that field to match the original. It looked like this:

db.events.find({timestamp: {$type: 2}})
    .forEach( function (e) {
        e.newTimestamp = new ISODate(e.timestamp);
        db.events.save(e);
    } )

db.events.update({},
    {$rename: {'newTimestamp': 'timestamp'}},
    {multi: true})

By the way, that method for converting the string times to ISODates was what ended up working. (I got the idea from this SO answer.)

like image 894
bdesham Avatar asked May 17 '13 13:05

bdesham


3 Answers

My advice would be to add the ISODate as a new field. Once confirmed that all looks good you could then unset the the string date.

like image 163
James Wahlin Avatar answered Nov 05 '22 07:11

James Wahlin


Create a test environment with your database structure. Copy a handful of records to it. Problem solved. Not the solution you were looking for, I'm sure. But, I believe, this is the exact circumstances that a 'test environment' should be used for.

like image 28
eidsonator Avatar answered Nov 05 '22 06:11

eidsonator


Select ID of particular records that you would like to monitor. place in the update {_id:{$in:[<your monitored id>]}}

like image 2
Dewfy Avatar answered Nov 05 '22 05:11

Dewfy