如何在流星中重命名集合? [英] How to rename a collection in meteor?
问题描述
我想从代码而不是从Shell重命名集合,但是我没有找到命令, 我知道
I want to rename a collection from code, not from shell, but I find no command, I know
db.originalCollectionName.renameCollection('newCollectionName')
但这仅来自shell
but it's only from shell
推荐答案
由于Meteor是在NodeJS之上构建的,因此您可以使用任何NodeJS包和API.就您而言,您可以使用 Child Process API .
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.
Gentlenode的创始人朱利安(Julien)给了这个答案并写了这个
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);
在您的代码中,您将使用mongo --eval "yourcommand()"
.可以在文档. (如果您已经在计算机上使用了MongoDB,而没有使用Meteor提供的MongoDB,则此方法将起作用.)
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.)
这篇关于如何在流星中重命名集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!