美文网首页
mongo导入js脚本(Mongo命令)遇到的问题

mongo导入js脚本(Mongo命令)遇到的问题

作者: mtinsky | 来源:发表于2022-04-21 10:55 被阅读0次

    记录在linux下执行mongo导入js脚本(Mongo命令)遇到的问题

    1. 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
    
    1. 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
    
    1. 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
    
    1. Mongodb的js脚本操作多个数据库
    • 使用db.getSiblingDB( '数据库名' )或者db.getSisterDB( '数据库名' )
    • 例子
    # 加上有数据库dba和dbb,分别有集合ca,cb
    db.getSiblingDB( 'dba' ).ca.find();
    db.getSiblingDB( 'dbb' ).cb.find();
    

    相关文章

      网友评论

          本文标题:mongo导入js脚本(Mongo命令)遇到的问题

          本文链接:https://www.haomeiwen.com/subject/vidiertx.html