afloyd / mongo-migrate

MIT License
159 stars 81 forks source link

Migration is only updating first record in a collection #61

Closed rdnorthup closed 8 years ago

rdnorthup commented 8 years ago

I'm trying to do a relatively simple operation and it is only updating the first record in the collection. This is my first time using mongo-migrate so perhaps I'm doing something wrong....

var mongodb = require('mongodb');

exports.up = function(db, next){
    db.collection('users').find().forEach(function(user) {
        console.log('migrating user: ' + user.emails[0].address);
        user.profile.normalizedEmail = user.emails[0].address.toLowerCase();
        db.collection('users').save(user);
    });

    next();
};
rdnorthup commented 8 years ago

I should add that running the same code from the mongo shell, with db.collection('users') changed to db.users, and print instead of console.log, works fine.