Mongodb shell脚本
来源:互联网 发布:微信公众平台安全域名 编辑:程序博客网 时间:2024/06/03 23:42
原文地址:https://docs.mongodb.com/manual/mongo/
The mongo Shell
On this page
- Introduction
- Start the mongo Shell
- Working with the mongo Shell
- Tab Completion and Other Keyboard Shortcuts
- Exit the Shell
Introduction
The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations.
The mongo shell is a component of the MongoDB distributions. Once you have installed and have started MongoDB, connect the mongo shell to your running MongoDB instance.
Most examples in the MongoDB Manual use the mongo shell; however, many drivers provide similar interfaces to MongoDB.
Start the mongo Shell
IMPORTANT
Ensure that MongoDB is running before attempting to start the mongo shell.
To start the mongo shell and connect to your MongoDB instance running on localhost with default port:
At a prompt in a terminal window (or a command prompt for Windows), go to your <mongodbinstallation dir>:
cd <mongodb installation dir>
Type ./bin/mongo to start mongo:
./bin/mongo
If you have added the <mongodb installation dir>/bin to the PATH environment variable, you can just type mongo instead of ./bin/mongo.
Options
When you run mongo without any arguments, the mongo shell will attempt to connect to the MongoDB instance running on the localhost interface on port 27017. To specify a different host or port number, as well as other options, see examples of starting up mongo and mongo reference which provides details on the available options.
.mongorc.js File
When starting, mongo checks the user’s HOME directory for a JavaScript file named .mongorc.js. If found, mongo interprets the content of .mongorc.js before displaying the prompt for the first time. If you use the shell to evaluate a JavaScript file or expression, either by using the --eval option on the command line or by specifying a .js file to mongo, mongo will read the .mongorc.js file after the JavaScript has finished processing. You can prevent .mongorc.js from being loaded by using the --norc option.
Working with the mongo Shell
To display the database you are using, type db:
db
The operation should return test, which is the default database. To switch databases, issue the use <db>helper, as in the following example:
use <database>
To list the available databases, use the helper show dbs. See also db.getSiblingDB() method to access a different database from the current database without switching your current database context (i.e. db).
You can switch to non-existing databases. When you first store data in the database, such as by creating a collection, MongoDB creates the database. For example, the following creates both the database myNewDatabase and the collection myCollection during the insert() operation:
use myNewDatabasedb.myCollection.insert( { x: 1 } );
The db.myCollection.insert() is one of the methods available in the mongo shell
- db refers to the current database.
- myCollection is the name of the collection.
If the mongo shell does not accept the name of the collection, for instance if the name contains a space, hyphen(连字符), or starts with a number, you can use an alternate syntax to refer to the collection, as in the following:
db["3test"].find()db.getCollection("3test").find()
For more documentation of basic MongoDB operations in the mongo shell, see:
- Getting Started Guide
- Insert Documents
- Query Documents
- Update Documents
- Delete Documents
- mongo Shell Methods
Format Printed Results
The db.collection.find() method returns a cursor to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Typeit to iterate another 20 times.
To format the printed result, you can add the .pretty() to the operation, as in the following:
db.myCollection.find().pretty()
In addition, you can use the following explicit print methods in the mongo shell:
- print() to print without formatting
- print(tojson(<obj>)) to print with JSON formatting and equivalent to printjson()
- printjson() to print with JSON formatting and equivalent to print(tojson(<obj>))
For more information and examples on cursor handling in the mongo shell, see Iterate a Cursor in the mongo Shell. See also Cursor Help for list of cursor help in the mongo shell.
Multi-line Operations in the mongo Shell
If you end a line with an open parenthesis ('('), an open brace ('{'), or an open bracket ('['), then the subsequent lines start with ellipsis ("...") until you enter the corresponding closing parenthesis (')'), the closing brace ('}') or the closing bracket (']'). The mongo shell waits for the closing parenthesis, closing brace, or the closing bracket before evaluating the code, as in the following example:
> if ( x > 0 ) {... count++;... print (x);... }
You can exit the line continuation mode if you enter two blank lines, as in the following example:
> if (x > 0......>
Tab Completion and Other Keyboard Shortcuts
The mongo shell supports keyboard shortcuts. For example,
Use the up/down arrow keys to scroll through command history. See .dbshell documentation for more information on the .dbshell file.
Use <Tab> to autocomplete or to list the completion possibilities, as in the following example which uses <Tab> to complete the method name starting with the letter 'c':
db.myCollection.c<Tab>
Because there are many collection methods starting with the letter 'c', the <Tab> will list the various methods that start with 'c'.
For a full list of the shortcuts, see Shell Keyboard Shortcuts
Exit the Shell
To exit the shell, type quit() or use the <Ctrl-c> shortcut.
SEE ALSO
- Getting Started Guide
- mongo Reference Page
- Mongodb shell脚本
- mongodb shell 执行 js 脚本
- mongodb的shell执行脚本-js脚本
- mongodb操作命令------shell脚本启动-插入
- mongodb常用的shell脚本语法
- mongodb执行js脚本(一)---shell执行
- 如何通过shell脚本操作MongoDB
- Mongodb总结1-启动和Shell脚本
- mongodb在shell运行javascript脚本
- MongoDB 的shell脚本基础进阶
- mongodb 3.0.4 shell安装卸载脚本
- 用shell脚本操作mongodb更新
- MongoDB文档翻译-mongo Shell-给mongo Shell编写脚本
- MongoDB shell脚本 自动添加权限与账号密码
- 批量导入json到mongodb的shell脚本
- mongodb数据库备份和利用shell脚本编写
- 一键导出导入mysql&mongodb shell脚本
- mongodb shell 运行js脚本的四种方式
- 分布式集群中大数据的中位数
- Spring整合log4j
- 【八】顺序最小优化算法
- poj 2084 Game of Connections (卡特兰数、大数)
- SIGHASH_NONE, SIGHASH_SINGLE 和SIGHASH_ALL
- Mongodb shell脚本
- class文件如何得知jdk编译版本
- 第九周项目一--猴子选大王(数组版)
- Path的常用方法
- 使用Reveal查看真机app的UI布局
- RxJava Android(RxAndroid) 开发全家桶
- Android源码解析之一Activity启动时界面绘制流程
- ThinkPHP框架学习之Image类
- 协方差的物理意义