mongodb database, a collection of data may be introduced.

We know that there inside the database in a relational database, data table, which is table data line by line. Mongodb the non-relational database, which some database, a collection of documents, which respectively correspond to the relational database, data tables, and the table inside the data line by line. In mongodb, the documents constitute a collection of a collection of database

Relationship consists of:

1. Document

This document is not usually say a word document, in which mongodb, the document refers to a collection inside a row of data. Look at what it looks like.

 

 

 Can be seen, a document consists of three parts, ID (line to represent a document), key (not a duplicate key file name appears and is case sensitive) and a key, the document line, may contain a plurality of keys values ​​can store different types of data.

2. collection

Collection is equivalent relational database table inside, consists of a collection of documents, you can use the show collections or show tables command to view a database which contains several collections, like this (and there are, but here is a look at)

 

 

 3. Database

Mysql database and on the inside is the same, but in mongodb inside, the database is composed of a collection, you can use the show dbs view the database has been created like this

 

 

 

 

 

Commonly used commands:

 

1. Database command:

Create / switch databases: use "database name"

 

The use command, if not followed by the name of the database, then create a new database, if followed by the name of an existing database, you need to switch to another database operations. You can use a pointer to imagine, if the database already exists, points to this database, if you do not exist, create it.

 

 

 New_data create a database using the db command to view the current database database pointer points,

 

 

But this time, use the command show dbs view does not display the newly created database, but will only go to the corresponding database created in the first insert a file to the database in time. At this point, the collection has similar characteristics. Create a collection in this database, the following command

db.createCollection ( "collection name")

 

 

 Then use the show dbs look at the database, there is a database just created new_data

 

 

 Delete the database: db.dropDatabase ()

Tryone delete the database already exists, as follows:

 

 

 First use the database using the pointer to tryone database, and then use the command to view the database db is currently operating, after using the delete command, and then view this database, you can see, tryone database has been deleted, Note: After deleting the database, the pointer point has not changed, then changed to use the pointer to use

2. collection command

View the collection: show collections / tables

Above, it has been referred to the creation of a set of commands: db.createCollection ( "collection name") , use it again to create a collection name, as follows:

 

 At this point we see the database already has name and two new collections

Delete command and then look at the collection, . Db collection name .drop () , as follows:

 

 When you delete a collection of successful returns true, to see again the set.

3. Document command

Insert documents:. Db collectionname .insert ({ "keys":} key)

new collection of the above into your document. as follows:

 

 Query documents: db collection name .find ().

Inserted above document query. as follows:

 

 Updated document: db.new.update ({ "name": "zhangsan"}, {$ the SET: { "name": "lisi"}})

After the first brace here is the update query, similar to the back where within sql update query.

 

 Again, the key has been changed.

Delete the document: . Db collection name .remove ()

 

(Ps: feel free to correct me ..... welcome Gangster) 

******************** short step a thousand miles ********************

 

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11615943.html