为什么变量'db'不是只读的?

时间:2012-03-23 17:27:56

标签: mongodb

将db设置为某个任意值后,我无法在shell中执行任何操作。

是否知道错误或预期的行为?

> use tutorial
switched to db tutorial
> db
tutorial

> db = 5
5
> db
5

> show dbs
Fri Mar 23 17:18:40 TypeError: db.getMongo is not a function shell/utils.js:1235
>
> use tutorial
Fri Mar 23 17:18:55 TypeError: db.getMongo is not a function shell/utils.js:1167
> db = 'tutorial'
tutorial
> show dbs
Fri Mar 23 17:19:38 TypeError: db.getMongo is not a function shell/utils.js:1235

1 个答案:

答案 0 :(得分:5)

Mongo Interactive Shell是一个Javascript Shell,因此它遵守Javascript Shell的所有规则。您将覆盖在启动期间初始化的db变量。

> a = db
SocialStreams
> db = "Hello"
Hello
> db.help()
Fri Mar 23 12:08:13 TypeError: db.help is not a function (shell):1
> db = a
SocialStreams
> db.help()
DB methods:
    db.addUser(username, password[, readOnly=false])
    db.auth(username, password)
    db.cloneDatabase(fromhost)
...