renameCollection with Meteor

How do I use renameCollection with Meteor? I would like to do this inside Meteor because I need do migrations for development and production environment.

Check this out: https://github.com/meteor/meteor/issues/4947 and show your support there, perhaps.

For a given Mongo.Collection instance, you can call .rawCollection() and then call .renameCollection() after it (in addition to any other native MongoDB API). It only works on the server.

var myCollection = new Mongo.Collection('myCollection');
myCollection.rawCollection().renameCollection(/* ... */);

PS - I don’t remember if this has changed yet, but if you’re using an older version of Meteor you might need to use .rename() (MongoDB 1.4) instead of .renameCollection() (MongoDB 2.x).

1 Like

myCollection.rawCollection().renameCollection(/* ... */); gives me
TypeError: Object [object Object] has no method 'renameCollection'

and
myCollection.rawCollection().rename(/* ... */); gives me
TypeError: Cannot read property 'dropTarget' of undefined

You need to use this API:

http://mongodb.github.io/node-mongodb-native/1.4/api-generated/collection.html#rename

Decided to manually rename the collection… Can’t believe that there’s no straight forward solution to this.

The following worked for me.

    const collection = new Mongo.Collection('<old-name>')
    collection.rawCollection().rename('<new-name>')