I want to rename a collection from code, not from shell, but I find no command, I know
db.originalCollectionName.renameCollection('newCollectionName')
but it's only from shell
I want to rename a collection from code, not from shell, but I find no command, I know
db.originalCollectionName.renameCollection('newCollectionName')
but it's only from shell
Since Meteor is built on top of NodeJS, you can use any NodeJS packages and APIs. In your case, you can run your rename command using bash commands in your code using the Child Process API.
Julien, the founder of Gentlenode, gave this answer and wrote this post to describe the process in more details. It is copied below for your convenience.
exec = Npm.require('child_process').exec;
child = exec('ls -la', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
}
// More concisely
runCommand = function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
}
exec("ls -la", runCommand);
In your code, you'd use mongo --eval "yourcommand()"
. More options can be found in the docs. (This will work provided you use the MongoDB already on your machine, and not the one Meteor provides.)