记录在linux下执行mongo导入js脚本(Mongo命令)遇到的问题
- Unauthorized
- 现象
执行./mongo < test.js,如下报错
MongoDB shell version v3.4.23
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.23
2022-04-21T10:35:57.062+0800 E QUERY [thread1] Error: error: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { xxxxxxx }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:702:1
DBQuery.prototype._exec@src/mongo/shell/query.js:117:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:288:5
@(shell):1:7
2022-04-21T10:35:57.063+0800 E QUERY [thread1] TypeError: this._cursor is null :
DBQuery.prototype.close@src/mongo/shell/query.js:681:5
@(shell):1:1
bye
- unterminated string literal
- 现象
执行./mongo < test.js,如下报错
MongoDB shell version v3.4.23
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.23
2022-04-21T09:11:06.308+0800 E QUERY [thread1] SyntaxError: unterminated string literal @(shellhelp1):1:24
error2:SyntaxError: unterminated string literal @(shellhelp1):1:24
bye
- console is not defined
- 现象
执行./mongo < test.js,如下报错
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.23
2022-04-21T09:29:10.270+0800 E QUERY [thread1] ReferenceError: console is not defined :
@(shell):31:2
bye
- Mongodb的js脚本操作多个数据库
- 使用db.getSiblingDB( '数据库名' )或者db.getSisterDB( '数据库名' )
- 例子
# 加上有数据库dba和dbb,分别有集合ca,cb
db.getSiblingDB( 'dba' ).ca.find();
db.getSiblingDB( 'dbb' ).cb.find();
网友评论