博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB - Introduction of the mongo Shell
阅读量:5250 次
发布时间:2019-06-14

本文共 5406 字,大约阅读时间需要 18 分钟。

Introduction

The  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 . Once you have installed and have started MongoDB, connect the mongo shell to your running MongoDB instance.

Most examples in the  use the mongo shell; however, many  provide similar interfaces to MongoDB.

 

Start the mongo Shell

IMPORTANT: Ensure that MongoDB is running before attempting to start the o shell.

To start the mongo shell and connect to your MongoDB instance running on localhost with default port:

  1. At a prompt in a terminal window (or a command prompt for Windows), go to your <mongodbinstallation dir>:
    cd 
  2. Type ./bin/mongo to start :
    ./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  and  which provides details on the available options.

.mongorc.js File

When starting,  checks the user’s  directory for a JavaScript file named . If found, 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 ,  will read the .mongorc.js file after the JavaScript has finished processing. You can prevent .mongorc.js from being loaded by using the  option.

 

Working with the mongo Shell

To display the database you are using, type db:

> dbtest

The operation should return test, which is the default database. To switch databases, issue the use <db> helper, as in the following example:

> use mydbswitched to db mydb

To list the available databases, use the helper show dbs. See also  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 databasemyNewDatabase and the  myCollection during the  operation:

> use myNewDatabaseswitched to db myNewDatabase> db.myCollection.insert( { x: 1 } );2016-11-30T22:43:53.953+0800 I COMMAND  [conn3] command myNewDatabase.myCollection command: insert { insert: "myCollection", documents: [ { _id: ObjectId('583ee5a99efcdb65c8842e89'), x: 1.0 } ], ordered: true } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } } } protocol:op_command 280msWriteResult({ "nInserted" : 1 })

The  is one of the 

  • 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:

Format Printed Results

The  method returns a  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 Type it 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  formatting and equivalent to printjson()
  • printjson() to print with  formatting and equivalent to print(tojson(<obj>))

For more information and examples on cursor handling in the mongo shell, see . See also  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:

> var x = 1> var c = 0> if (x > 0) {... c++;... }0> c1

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  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':

 

Exit the Shell

To exit the shell, type quit() or use the <Ctrl-c> shortcut.

 

转载于:https://www.cnblogs.com/huey/p/6120141.html

你可能感兴趣的文章
VS2015复制VS2013的项目,编译报错
查看>>
如何有效的思考
查看>>
scala学习笔记:match与unapply()
查看>>
目录操作
查看>>
[MTK FP]如何通过ICON ID的value找到对应的ICON
查看>>
KindEditor在线HTML文本编辑器在asp.net中的使用
查看>>
Django的ORM实现数据库事务操作
查看>>
数理方程:Laplace变换 & 留数(更新中)
查看>>
Centos 6.9 install Python3.7
查看>>
laydate 显示结束时间不小于开始时间
查看>>
C# 网上收集的一些所谓的开源项目
查看>>
ASP.NET在IIS7中如何更改网站的.net framework框架版本
查看>>
6月19 琐碎知识点
查看>>
HTML5常用的方法
查看>>
第一个 MVC 应用程序(下半部分)
查看>>
urllib爬虫(流程+案例)
查看>>
JS基础_while的练习2
查看>>
Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局...
查看>>
Oracle O立方服务平台(O3SP)
查看>>
clion 查看代码 多次查看后如何一步一步回退到最初查看的代码位置
查看>>