The basic operation of the database DB

Start MongoDB service 
sudo service mongodb start

MongoDB enter the command line interface, knocking exit at the command line to exit.
mongo

Create a database 
use

db command to view your currently selected database:
Show dbs command to view all of the database

Delete the database
syntax: db.dropDatabase ()
> Show dbs # displays the current system in all database
> use Chenshi # Go Chenshi database
Switched to db Chenshi
> db.dropDatabase ()
{ "dropped Total": "Chenshi", "the ok" :. 1}
> Show DBS

Create a collection
Syntax: db.createCollection (name, options)

Parameter Description:

name: the name of the collection created
options: as an initialization document (optional)
> db.createCollection ( "shiyanlou") # no arguments
{ "the ok": 1}
> Show the Collections
shiyanlou
system.indexes
> db.createCollection ( " shiyanlou2 ",
 {the capped:. 1, autoIndexId:. 1, size: 6.1428 million, max: 10000}) # parameters
{" ok ": 1}
parameters:

capped: of type Boolean, if create a fixed size set to true, when it reaches the maximum entry may automatically overwrite the previous entry. In setting its parameters should be specified as true size;
autoIndexId: type Boolean, default is false, if set to true, it will automatically create an index on _id field;
size: If you need to specify capped true, specifies parameters maximum, the unit is byte;
max: Specifies the maximum number of documents.
Also you can not create a collection in Mongodb, because the collection will be created automatically when you create a document.

插入文档
语法:db.COLLECTION_NAME.insert(document)
> userdoc1=({"user_id":1,"name":"cloud","state":"active","actor":"user","e-mail":"[email protected]","VM_num":2,"time":[{"date":"2014-08-12","hour":"10:53 PM"}] })
> userdoc2=({"user_id":2,"name":"testadmin","state":"active","actor":"admin","e-mail":"[email protected]","VM_num":2,"time":[{"date":"2014-08-11","hour":"06:34 AM"}] })
> doc1=({"name":"peter","position":"teacher"})    #先定义文档

> use Chenshi
switched to db Chenshi
> db.shiyanlou.insert(userdoc1)
WriteResult({"nInserted":1})
> db.shiyanlou.insert(userdoc2)
WriteResult({"nInserted":1})
> db.shiyanlou.insert(doc1)
WriteResult({"nInserted":1})

Alternatively existing document
Syntax: db.COLLECTION_NAME.save ({_ id: ObjectId (), NEW_DATA})
Operation Example:

> db.shiyanlou.save({"_id":ObjectId("53ea174ccb4c62646d9544f4"),"name":"Bob","position":"techer"})
WriteResult({"nMatched":1,"nUpserted":1,"nModified":1})

Update documents
Syntax: db.COLLECTION_NAME.update (SELECTION_CRITERIA, UPDATED_DATA)
Walkthrough:

> Db.shiyanlou.update ({ "user_id": 2, "e-mail": "[email protected]"}, {$ set: { "e-mail": "[email protected]"}})
WriteResult ({ "nMatched":. 1, "nUpserted":. 1, "nModified":}. 1)
> db.shiyanlou.find ()
the document user_id = 2 to the e-mail [email protected]
first braces indicate content search conditions, the second braces content updated data indicates
the default update function only update a document, if you want the role of all the documents, you need to add multi: true
Walkthrough:

db.shiyanlou.update({"e-mail":"[email protected]"},{$set:{"e-mail":"[email protected]"}},{multi:true})

Deleting a document
syntax: db.COLLECTION_NAME.remove (DELECTION_CRITERIA)
> db.shiyanlou.remove ({ "name": "Bob"})
WriteResult ({ "nRemoved": 1})
In fact, the first parameter with the update function remove function the same argument, the equivalent of the search conditions, be careful not to accidentally deleted!

After deleting can use the Find command to confirm data
> db.shiyanlou.find ()

Summary
createCollection (): create a collection
db.COLLECTION.drop (http://www.amjmh.com/v/): Delete collection
db.COLLECTION_NAME.insert (document): insert the document
db.COLLECTION_NAME.update (SELECTION_CRITERIA, UPDATED_DATA) : update document
db.COLLECTION_NAME.save ({_ id: ObjectId ( ), NEW_DATA}): replace existing document
db.COLLECTION_NAME.remove (DELECTION_CRITERIA): delete the document
-------------- -------

Guess you like

Origin www.cnblogs.com/ly570/p/11335170.html