I want to set a field on the basis of an existing field. Like:
db.attendances.find().forEach(function(attendance) {
var entryTime = attendance["entryTime"];
var exitTime = attendance["exitTime"];
if(entryTime != null) {
var entryTimeMillis = entryTime.getTime();
attendance["entryTimeMillis"] = entryTimeMillis;
}
if(exitTime != null) {
var exitTimeMillis = exitTime.getTime();
attendance["exitTimeMillis"] = exitTimeMillis;
}
db.attendances.save(attendance);
});
I have about 10 Million records in the attendances
collection. Is there a way I can execute this faster as this will take a lot of time for 10 Million records?
The function is just converting an existing ISODate
field to milliseconds
and saving it as another field.
After a comment, I tried bulkWrite
:
try {
db.attendances.bulkWrite(
[{
updateMany : {
"filter" : { "entryTime" : { $exists : true } },
"update" : { $set : { "entryTimeMillis" : "$entryTime.getTime()" } }
}
}]
);
} catch (e) {
print(e);
}
But of course "$entryTime.getTime()"
is not correct as that won't be evaluated and it doesn't work without the "
. What do I do?