NOSQL MONGO Introductory Learning note-Basic insertion of data (ii)

Source: Internet
Author: User
Tags mongo shell

After successfully running MONGO, the command line mode is entered, and the test database is selected by default MONGO

1. Use the DB command to print out the currently selected database:

> DB

Test

  

2. Use the show DBS command to print out a list of databases

> Show DBS

Local 0.078GB

3. Using use [dbname] to switch the database

> Use Local

Switched to DB Local

Try using the DB command to print a confirmation that is not in the local database:

> DB

Local

Even if use [anydb] can be used to switch the database when using this command, the database will not be created as long as no data is inserted into the library. You can use show DBS to view it!

4. Until now I'm still searching for installation problems, not writing any data to the database, and then slowly uncovering the veil.

MONGO does not have a data table, which is called a set, even a set of data. The collection does not need to be displayed for creation. The official said that the use of dynamic schemas, and do not need to insert data before the structure of the document definition.

Switch to a new database

Use MyDB

Create a document for two J and K commands:

j = {name: ' Test '}

k = {X:3}

inserting into the collection

Db.test.insert (j)

Db.test.insert (k)

Notice that the second execution is significantly faster than the first because the database and the collection are initialized the first time it is inserted.

When you insert the first document, MONGO creates both the MyDB database and the test collection.

  

Verify that the collection exists

> Show Collections

System.indexes

Test

All databases will contain a System.indexes collection

3. Looping through data

Loops can be used to traverse the data under the MONGO shell

var c = db.test.find ()//Get cursor

while (C.hasnext ()) Printjson (C.next ())//Print data

This will print the data in the collection, but note that the MONGO default display only 20 data

After getting the data, you can enter it to get the following 20 data

When you get the C object, you can also use the subscript to read the data

Printjson (C[2])

Find () can be used not only for collection objects, but also for lookups like SQL select

> Db.test.find ({x:3})

{"_id": ObjectId ("556ec46ee82c862a1abf7913"), "X": 3}

Limit the number of queries using limits ()

> Db.test.find (). Limit (3)//Return three collection

> Db.test.find ({x:3}). Limit (3)//Return three eligible collection data

4. In the MONGO script, you can use JavaScript scripts to loop to the collection

> for (var i =1; i<=; i++) Db.test.insert ({x:i})

Use Find () to view

> Db.test.find ()

  

NOSQL MONGO Introductory Learning note-Basic insertion of data (ii)

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.