afloyd / mongo-migrate

MIT License
159 stars 81 forks source link

Collection Undefined on a Simple Migration #63

Closed artforlife closed 7 years ago

artforlife commented 7 years ago

I am trying to do a very basic migration by adding a field to all documents. However, I keep getting an error "TypeError: Cannot read property 'update' of undefined". Here is my migration file:

'use strict';

module.exports = {

  up: function (db, next) {
    // TODO write your migration here
    db.Activity.update({}, {$set: {"runtime" : {
                                                    "started" : false,
                                                    "startDate" : null,
                                                    "endDate" : null
                                                }
                                   }
                            }, 
                        false, true);
    next();
  },

  down: function (db, next) {
    // TODO write the statements to rollback your migration (if possible)
    db.Activity.update({}, {$unset: {"runtime" : ""}}, false, true);
    next();
  }

};`

If I run the same queries in MongoDB shell (Robomongo) they work just fine.

afloyd commented 7 years ago

I haven't used Mongo or this library in a few years... But it looks like you need to first get your collection.

Try:

'use strict';

module.exports = {

  up: function (db, next) {
    // TODO write your migration here
   var Activity = db.Collection('Activity');
   Activity.update({}, {$set: {"runtime" : {
                                                    "started" : false,
                                                    "startDate" : null,
                                                    "endDate" : null
                                                }
                                   }
                            }, 
                        false, true);
    next();
  },

  down: function (db, next) {
    // TODO write the statements to rollback your migration (if possible)
    var Activity = db.Collection('Activity');
   Activity.update({}, {$unset: {"runtime" : ""}}, false, true);
    next();
  }

};`
artforlife commented 7 years ago

It still does not work.

TypeError: db.Collection is not a function

artforlife commented 7 years ago

If I console.log the db it outputs an object with some fields. However, this object has neither get nor Collection method.