A probe into the MongoDB database MongoDB command Getting Started

Source: Internet
Author: User
Tags mongodb server

MongoDB is a NoSQL database that uses the "set-oriented" (collection-oriented) principle, meaning that data is grouped in a dataset, called a collection (Collection). Each collection (Collection) has a unique identifying name in the database and can include an unlimited number of objects (Basicdbobject).

The concept of a collection (Collection) is similar to a table in a relational database (RDBMS), the concept of an object (Basicdbobject) is similar to a data in a table in an RDBMS. Inserting a data into a relational database is equivalent to adding a basicdbobject to the collection of MongoDB.

People who like Java are very quick to like MongoDB, because of its object-oriented operation to make people's eyes bright. In the previous article we introduced MongoDB installation and configuration process, this blog in a popular and concise way to introduce the MongoDB entry-level command.

1. Enter the MongoDB shell

Open the MongoDB service in Control Panel, and at the same time use the MONGO command to connect to the MongoDB server that is already started, and enter the MongoDB shell, for example, as seen:

2. Display the current database

>db

Test

3. Show all databases

>Show DBS

Admin (empty)
Andydb 0.078GB
Local 0.078GB

4. Switch Database (if the database does not exist, create a database)
> Useandydb

Switched to DB Andydb

5. Show All Tables (set collection)

>Show Collections

System.indexes

6. Create a table person and initialize the data

>db.person.insert ({name: "Andy", age:25})

Writeresult ({"ninserted": 1})

7. Insert a record

>db.person.save ({name: "Jack", age:50})

Writeresult ({"ninserted": 1})

8. Check all records

>db.person.find ()

{"_id": ObjectId ("537761762c82bf816b34e6ce"), "name": "Andy", "Age": 25}
{"_id": ObjectId ("537761DA2C82BF816B34E6CF"), "name": "Jack", "Age": 50}

9. Query a record

> db.person.find ({name: "Jack"})

{"_id": ObjectId ("537761DA2C82BF816B34E6CF"), "name": "Jack", "Age": 50}

> db.person.find ({$where: "this.name.length<10"})

{"_id": ObjectId ("537761762c82bf816b34e6ce"), "name": "Andy", "Age": 25}

{"_id": ObjectId ("537761DA2C82BF816B34E6CF"), "name": "Jack", "Age": 50}

10. Update a record

> db.person.update ({name: "Andy"},{$set: {age:100}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.person.find ({name: "Andy"})

{"_id": ObjectId ("537761762c82bf816b34e6ce"), "name": "Andy", "Age": 100}

11. Delete a record

> db.person.remove ({name: "Andy"})

Writeresult ({"nremoved": 1})

> Db.person.find ()

{"_id": ObjectId ("537761DA2C82BF816B34E6CF"), "name": "Jack", "Age": 50}

>db.person.remove ()//delete all records in the table

>db.person.drop ()//Delete table

12. View Help Commands

1) db.help () display the help command about the DB

>db.help ()

2) Db.collection.help () display the help command about collection

>db.person.help ()

3) db.collection.function.help () display Help commands about the collection method

> db.person.function.help ()

13. References

MongoDB Hello World Example ( recommended )

Getting Started with MongoDB

14. You may be interested in

How the MongoDB database installs, configures MongoDB

"MongoDB database" Java MongoDB CRUD Example

Reprint Please specify source: http://blog.csdn.net/andie_guo/article/details/26095367, thank you!

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.