mongo Shell脚本不会让我包含"use< database>" [英] mongo shell script won't let me include "use <database>"
问题描述
32位mongo 2.0.1
32-bit mongo 2.0.1 on a windows XP machine
//script filename: test.js (one line shell script file to store a person)
db.cTest.save({Name: "Fred", Age:21});
通过输入以下2个shell命令对数据库dbTest运行:
run against database dbTest by entering the following 2 shell commands:
> use dbTest
switched to dbTest
> load("test.js")
到目前为止,很好.
但是,如果我尝试在脚本中包含"use"语句,则会失败:
But if I try and include the "use" statement in the script it fails:
//script filename: test.js (including "use" statement)
use dbTest;
db.cTest.save({Name: "Fred", Age:21});
失败,并显示以下错误消息:
fails with error msg as follows:
> load("test.js")
SyntaxError: missing ; before statement
Mon Dec 19 11:56:31: Error: error loading js file temp.js (shell):1
在test.js中添加或删除分号似乎无关紧要.
Adding or removing semicolons to test.js doesn't seem to matter.
那么如何将"use"指令放入mongo shell脚本中?
So how do you put a "use" directive into a mongo shell script?
推荐答案
http://www.mongodb .org/display/DOCS/Scripting + the + shell
使用dbname
该命令在脚本模式下不起作用.相反,您将需要在连接中显式定义数据库(上例中为/dbname).
use dbname
This command does not work in scripted mode. Instead you will need to explicitly define the database in the connection (/dbname in the example above).
或者,您也可以在脚本中创建连接:
Alternately, you can also create a connection within the script:
db2 = connect("server:27017/otherdbname")
db2 = connect("server:27017/otherdbname")
这篇关于mongo Shell脚本不会让我包含"use< database>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!