Simple use of Mongodb

I. Basic Concepts

Database (database)

Collection (collection)

Document (document)

Three relationships: the database contains more than one collection, each set also includes multiple documents

In Mongodb, the database and do not need to manually create a collection, when we create a document, if the document where the collection or database will automatically create the database and collection does not exist

 

II. Basic Instructions

show databases (dbs): shows all the current database

use the database name: Go to the specified database

db: indicates the database is currently located

show collections: show all collection database

 

Three. CRUD database (CRUD) operations

1 to the database into a document (increase)

db <collection> .insert (doc):. insertion of one or more documents to the collection

When inserted into the document to the collection, if you do not specify _id attribute to the document, the database will automatically add the document _id (the property as a unique identifier of the document, known as a primary key in mysql), _ id we can specify your own, if we It specifies the data will not add up, if they specify _id, must determine its uniqueness

Example:

Insert a new set of test stus student objects to the library in

  db.stus.insert ({name: "Monkey", age: "18", gender: "M"})

Insert number of new students to stus objects in the library collection test

  db.stus.insert([

   {name:"张三", age:"18"},

   {name:"李四", age:"20"}

  ])

2. Query

(1)db.<collection>.find()

find () to query the set of all eligible document, it can accept an object as a parameter condition

{} Represents the set of all documents in the query, such as: db <collection> .find ({}).

Note, find () returns an array

{Attribute: value} value document specified query attribute as db.stus.find ({name: "San"})

(2)db.<collection>.findone():

To query the set of qualified first document

findone () returns a document object

(3)db.<collection>.findmany():

   To query all documents in the collection qualifying

3. Modify

db.collection.update (query, the new object), a new team to here means new content to be modified

It will use the new object under (1) update () default to replace the old objects

Such as: db.stus.update ({name: "Joe Smith"}, {age: 33}), this statement will be the seating of the name is modified to include only documents age: 33 documents, not in the source document only modify the properties on the basis of age

(2) To modify the properties developed, rather than replace, the need to use the "Edit operator" to complete the modification

     $ Set: can be used to modify specific properties of the document

  $ Unset: documents can be used to delete the specified property

Such as:

db.stus.update ( 
    {name: " Joe Smith " }, 
    {$ SET : { 
         name: "Pig" 
    }}            
)

After the run, Joe Smith was changed to a pig

(3)db.collection.updateMany():

  Edit multiple documents in line with the conditions

(4)db.collection.updateOne()

  Modify a qualifying document

4. Delete

(1)db.collection.remove()  :

Delete all qualifying documents (default next time delete multiple), if remove () a second parameter true, you delete only the qualifying documents, such as:

db.stus.remove({age: 28}, true)

If only pass an empty object as a parameter, all documents in the collection will be deleted

remove () You can also delete documents based on conditions and ways of delivery conditions and find () consistent

(2) db.collection.deleteOne (): delete a document

(3) db.collection.deleteMany (): Delete multiple documents

(4) db.collection.drop (): delete collections (this is higher than the efficiency delete documents, because deleting a file is to delete a file, then delete the collection directly)

(5) db.dropDatabase (): Delete Database

 

5. The relationship between the document and the document key

(1) One to One

(2) or many-to-many

(3) many to many

 

Guess you like

Origin www.cnblogs.com/jj1106/p/11262292.html