MongoDB MONGO Shell Common operation method and operation script notes _mongodb

Source: Internet
Author: User
Tags mongodb tojson create database mongo shell

First, the common command

1. Help View command Prompt

Help

Db.help ();

Db.yourColl.help ();

Db.youColl.find (). Help ();

Rs.help ();

2. Switch/CREATE Database

Use Yourdb; The current database is created automatically when a collection (table) is created
3, query all databases

Show DBS;

4, delete the current use of the database

Db.dropdatabase ();

5, clone the database from the designated host

 
 

Clones data from the database on the specified machine to the current database
6. Copy the specified database data to a database from the specified machine

Db.copydatabase ("MyDB", "temp", "127.0.0.1");

Copy the MyDB data from this computer to the TEMP database
7, repair the current database

Db.repairdatabase ();

8. View the database currently in use

Db.getname ();

db DB and GetName methods are the same effect, you can query the currently used database
9, display the current DB status

Db.stats ();

10, the current DB version

Db.version ();

11, view the current DB link machine address

Db.getmongo ();

Second, Collection (table) aggregation set

1. Create a Clustered collection (table)

Db.createcollection ("Collname", {size:20, Capped:5, max:100});//Create success will show {"OK": 1}

2, get the specified name of the aggregation set (table)

Db.getcollection ("account");

3, get the current db of all the aggregation set

Db.getcollectionnames ();

4. Displays the status of all clustered indexes in the current DB

Db.printcollectionstats ();

Third, user-related

1. Add a user

Db.adduser ("name");

Db.adduser ("UserName", "pwd123", true); 

Add user, set password, read only
2. Show all current users

Show Users;

3, delete the user

Db.removeuser ("UserName");

These are some of the most basic commands that I took as notes. Deeper crud I haven't even tried until I try to write again.

Statement block operations

1, simple Hello World

Print ("Hello world!");

This type of writing invokes the print function, and writes directly to "Hello world!" The effect is the same;

2. Convert an object to JSON

Tojson (New Object ());

Tojson (New Object (' a '));

3. Recycle Add Data

for (var i = 0; i < i++) {

... db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});

... };

This allows you to add 30 of data to the loop, but also omit the notation of parentheses.

for (var i = 0; i < i++) Db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});

is also possible, when you use Db.users.find () query, display more than one page of data, you can use it to view the next page of information;

4, Find cursor query

var cursor = Db.users.find ();

while (Cursor.hasnext ()) {

Printjson (Cursor.next ());

}

This allows you to query all the users information, as well as write

var cursor = Db.users.find ();

while (Cursor.hasnext ()) {Printjson (cursor.next);}

You can also omit the {} number

5. Foreach Iterative cycle

Db.users.find (). ForEach (Printjson);

foreach must pass a function to handle the data information for each iteration

6, the Find cursor when the array processing

var cursor = Db.users.find ();

CURSOR[4];

Get the data for the subscript index of 4

Since it can be treated as an array, it can be obtained in length: Cursor.length () or cursor.count ();

So we can also use the loop to display the data.

for (var i = 0, Len = c.length (); i < Len; i++) Printjson (c[i));

7. Convert find cursor to array

var arr = Db.users.find (). ToArray ();

Printjson (arr[2]);

Convert it to an array by using the ToArray method

8. Customize our own query results

Show only age <= 28 and show only age this column of data

Db.users.find ({age: {$lte:}}, {age:1}). ForEach (Printjson);

Db.users.find ({age: {$lte:}}, {age:true}). ForEach (Printjson);

Exclude an Age column

Db.users.find ({age: {$lte:}}, {Age:false}). ForEach (Printjson);

9, the foreach transfer function display information

Db.things.find ({x:4}). ForEach (function (x) {print (Tojson (x));});

Other

1, the error message before the query

Db.getpreverror ();

2. Clear Error Record

Db.reseterror ();

3. Display Database list

Show DBS

4, display the collection in the current database (similar to tables in the relational database)

Show collections

5. Display user

Show Users

6, switch the current database, this and ms-sql inside the same meaning

use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.