codecombat/scripts/mongodb/migrations/2016-04-15-move-deleted-classroom-members.js
2016-04-29 13:12:39 -07:00

21 lines
710 B
JavaScript

load('bower_components/lodash/dist/lodash.js');
var classrooms = db.classrooms.find();
classrooms.forEach(function (classroom) {
print('classroom', classroom._id);
classroom.members.forEach(function (userID) {
if (_.isString(userID)) {
userID = ObjectId(userID);
}
var user = db.users.findOne({ _id: userID }, { deleted: true });
if (user.deleted) {
print('\tFOUND ONE', userID);
print('\t', db.classrooms.update(
{ _id: classroom._id },
{
$addToSet: { deletedMembers: userID },
$pull: { members: userID }
}
));
}
});
});