windows XP机器上的32位mongo 2.0.1

//script filename: test.js  (one line shell script file to store a person)
db.cTest.save({Name: "Fred", Age:21});

通过输入以下两个shell命令来运行数据库dbTest:

    > use dbTest
    switched to dbTest
    > load("test.js")

到目前为止,一切顺利.

但如果我try 在脚本中包含"use"语句,它就会失败:

//script filename: test.js  (including "use" statement)
use dbTest;
db.cTest.save({Name: "Fred", Age:21});

失败,错误消息如下:

    > load("test.js")
    SyntaxError: missing ; before statement
    Mon Dec 19 11:56:31: Error: error loading js file temp.js (shell):1

添加或删除要测试的分号.js似乎并不重要.

那么,如何将"use"指令放入mongo shell脚本中呢?

推荐答案

http://www.mongodb.org/display/DOCS/Scripting+the+shell

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

或者,也可以在脚本中创建连接:

db2=connect("服务器:27017/otherdbname")

Mongodb相关问答推荐

MongoDB索引使用

当日期和时间在不同键的字符串中时,Mongo 查询过滤今天的数据

如何使用内部数组中的值更新文档

如何在 mongodb 查找的外键中使用正则表达式

通过insertId MongoDB获取文档

MongoDb 聚合未正确分组

MongoDB C# 驱动程序 2.0 InsertManyAsync 与 BulkWriteAsync

替换 MongoDB 中数组中的嵌入文档

子文档上的mongoose唯一索引

Mongo 重启错误 -- /var/run/mongodb/mongod.pid 存在

ExpressJS & Mongoose REST API struct :最佳实践?

有没有办法为 mongoose.js 聚合提供 allowDiskUse 选项?

如何从 node.js 以编程方式执行 mongodump 命令?

使用 Spring Security + Spring 数据 + MongoDB 进行身份验证

带有 Java 驱动程序的 MongoDB 聚合

Flask:设置应用程序和请求特定的属性?

MongoDB:查询和检索嵌入式数组中的对象?

MongoDB如何判断是否存在

从 mongo 结果中删除 _id

MongoDb 连接被拒绝