当前位置:Gxlcms > 数据库问题 > MongoDB Shell

MongoDB Shell

时间:2021-07-01 10:21:17 帮助过:10人阅读

connect("localhost:27017"); // 连接数据库 db = db.getSiblngDB("test"); // 选择数据库 cursor = db.demo.find(); // 查询集合 while(cursor.hasNext()) // 迭代输出结果 { printjson( cursor.next() ) }

直接运行

# (anaconda3) whoami at iamwho in [~]
$ mongo --quiet testMongoShell.js 
{ "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
{ "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
{ "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }

# (anaconda3) whoami at iamwho in [~]
$ mongo testMongoShell.js 
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
connecting to: localhost:27017
MongoDB server version: 3.4.10
{ "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
{ "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
{ "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }

交互式运行

1 > load("testMongoShell.js")
connecting to: localhost:27017
MongoDB server version: 3.4.10
{ "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 }
{ "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 }
{ "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }
true
  • 执行命令行程序

    例如:run("ls")

    2 > run("ls")
    2018-02-05T23:15:05.650+0800 I - [thread1] shell: started program (sh5361):  /bin/ls
    sh5361| 1.json
    sh5361| AnacondaProjects
    sh5361| avimrc
    3 > run("cd avimrc")
    2018-02-05T23:15:27.155+0800 I - [thread1] shell: started program (sh5366):  cd avimrc
    sh5366| Unable to start program cd avimrc: No such file or directory
    255

    似乎也只能运行内置的shell命令,但是也有着很多的限制.

  • .mongorc.js 文件

    mongo启动配置文件

    • 文件分为Global和Local,前者在/etc/.mongorc.js,后者在~下面
    • 可以配置命令提示符,具体参考这里
  • 编辑复合变量EDITOR

    这个我忽略了,直接在shell配置文件中export一个EDITOR不就行了吗

    在shell中使用edit + fileName or doc;即可

  • 更多命令可参考

    这里

  • MongoDB Shell

    标签:mongo   markdown   命令提示符   cti   UI   whoami   current   logs   user   

    人气教程排行