MongoDB Basics-Database and collection basic operations

Source: Internet
Author: User

    This note aims to learn and summarize MongoDB for the HTTPS://WWW.SHIYANLOU.COM/COURSES/RUNNING/50 website. 1, Start MongoDBbecause the MongoDB service does not start up with the system, you need to start the MongoDB service first:
    1. shiyanlou:~/$ sudo service MongoDB start

2, Introduction to the database
    • A mongodb can create multiple databases.
    • Use show DBS to view a list of all databases
    • The Execute DB command can view the current database object or collection.
    • You can connect to the specified database by running the use command
To connect to a database:
    1. shiyanlou:~/ $ MONGO> show DBSLOCAL0.078125GB
-1, documentationThe document is the core of MongoDB, similar to a row of data in a relational database. Multiple keys are placed together with their associated values as documents. Use a JSON-like Bson to store data in MongoDB. Bson can add some data types that are not JSON based on JSON. such as:
    1. {"company":"chenshi keji"}
-2, Document logic relationship:
    1. 嵌入关系:A文档中存在B文档。
    2.     引用式关系:两个文档分卡,A文档通过引用B文档的ID来实现关联。
-3, setA collection is a combination of a set of documents, equivalent to a table in a relational database.
    1. {"company":"chenshi keji"} {"people":"man","name" :"Peter"}
-4, Meta datadatabase information is stored in the collection, and they use the system's namespace uniformlydbname.system.* DBNAME can be replaced with DB or database name.
    1. DBNAME.system.namespaces: List all namespaces DBNAME.system.indexs: List all indexes DBNAME.system.profile: List Database profile DBNAME.system.users : List the users accessing the database DBNAME.system.sources: list server information

2, creation and destruction of databases-1, CREATE DATABASEusing the use command bedstead database
    1. Use MyDB
View the currently connected database
    1. Db
View all databases
    1. Show DBS
MyDB is not displayed in the listed database because MyDB is empty.-2, destroy the database.
    1. > db.dropdatabase () {"dropped":"local","  OK":1}
3, the collection is created and destroyed.-1, create collection
    1. > use mydbswitched to db mydb> Db.createcollection ("users") {"  OK":1}>
View Collections
    1. > show Collectionssystem.indexesusers>
-2, delete collection
    1. > db.users.drop ()true>
4, inserting data into the collectionInsert when inserting data, if the Users collection is not created, it is created automatically.
    1. > Db.users.insert ([{name:'jam', email:'[email protected]' },{name:'Tom', email:'[email protected]q.email' }])
Save When you insert data, the Users collection is created automatically if it is not created.
    1. > Db.users.save ([{name:'jam', email:'[email protected]'} , {name:'Tom', email:'[email protected]q.email' }])

From for notes (Wiz)

MongoDB Basics-Database and collection basic operations

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.