用Meteor重命名收藏

时间:2016-02-02 20:42:48

标签: mongodb meteor

如何将renameCollection与Meteor一起使用?我想在Meteor中做这个,因为我需要进行迁移以用于开发和生产环境。

1 个答案:

答案 0 :(得分:0)

使用RemoteCollectionDriver访问本机mongo驱动程序和实例administraion命令,包括 renameCollection 命令。

以下示例将test数据库中名为orders的集合重命名为orders2016数据库中的test

var mongoDriver = MongoInternals.defaultRemoteCollectionDriver(), // or Meteor._RemoteCollectionDriver
    db = mongoDriver.mongo.db;

// for commands not natively supported by the driver - https://docs.mongodb.org/manual/reference/command/
db.command({ renameCollection: "test.orders", to: "test.orders2016" }, function(error, result) {
    if (error) throw error;
    if (result.errmsg) {
        console.error('Error calling native renameCollection command:', result.errmsg);
    }
    else {
        console.log(result);
    }
});

要实现此服务器端,您可以遵循此异步模式:

var shell = function () {
    var Future = Npm.require('fibers/future'),
        future = new Future(),
        db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

    db.command({ renameCollection: "test.orders", to: "test.orders2016" }, 
        function(error, result) {
            if (err) throw new Meteor.Error(500, "failed");
            future.return(result);
        }
    );
    return future.wait();
};