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:
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:
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:
{"company":"chenshi keji"}
-2, Document logic relationship:
嵌入关系:A文档中存在B文档。
引用式关系:两个文档分卡,A文档通过引用B文档的ID来实现关联。
-3, setA collection is a combination of a set of documents, equivalent to a table in a relational database.
{"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.
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
Use MyDB
View the currently connected database
Db
View all databases
Show DBS
MyDB is not displayed in the listed database because MyDB is empty.-2, destroy the database.
> db.dropdatabase () {"dropped":"local"," OK":1}
3, the collection is created and destroyed.-1, create collection
> use mydbswitched to db mydb> Db.createcollection ("users") {" OK":1}>
View Collections
> show Collectionssystem.indexesusers>
-2, delete collection
> db.users.drop ()true>
4, inserting data into the collectionInsert when inserting data, if the Users collection is not created, it is created automatically.
> 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.
> 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