Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[teacher Zhao Qiang] use MongoDB's command line tool: mongoshell

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Start the mongo shell.

After MongoDB is installed, execute the following command directly from the command line terminal:

mongo

As shown below:

Optional parameters are as follows:

It can also be abbreviated as:

To use an external editor in the mongo shell, such as vi, simply set the environment variable:

export EDITOR=vi

Start mongo shel. Here we edit a function in mongoshell:

Definition function:

function myFunction () { }

Edit functions:

edit myFunctionfunction myFunction() {print("Hello World") } Second, mongoshell startup configuration file.mongorc.js

When started, mongo checks for a JavaScript file named.mongorc.js in the user's HOME directory. If found, mongo parses the contents of. mongorc.js before first displaying the prompt. If you execute a JavaScript file or evaluate an expression using the shell, 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 been processed. You can prevent. mongorc.js from loading by using the-norc option.

Create a prompt for the number of actions issued with the current session, defining the following variables:

cmdCount = 1;prompt = function() { return "mongo " + (cmdCount++) + "> ";} The effect is as follows:

Display database name and host name in mongo shell:

host = db.serverStatus().host;cmdCount = 1;prompt = function() { return db+"@"+host+" " + (cmdCount++) + "> ";} The effect is as follows:

Basic Operation of Mongoshell

To display the current database name, enter the following command. The test database is returned, which is the default database:

db

Switch database:

use

View all available databases:

show dbs or show databases

We can switch to a database that doesn't exist. MongoDB automatically creates the database the first time we store it. For example, the following example inserts a piece of data that creates both database (myNewDatabase) and collection (myCollection).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report