2

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

styvane
  • 59,869
  • 19
  • 150
  • 156
DavidFM
  • 193
  • 7

1 Answers1

0

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.)

Community
  • 1
  • 1
d4nyll
  • 11,811
  • 6
  • 54
  • 68
  • but mongo is insert into meteor? i must type "meteor mongo" for access to shell mongo and --eval don't exit. i miss something? sorry for my bad english :p – DavidFM Mar 16 '15 at 08:24
  • 1
    My solution is not perfect because it requires you to run the native MongoDB, instead of the one provided by Meteor, which (and anyone can correct me if I am wrong) is different. You can see how to run your own Mongo [here](http://stackoverflow.com/q/10588038/3966682) – d4nyll Mar 17 '15 at 04:42