Create a collectioncreatecollection ()
<span style= "FONT-SIZE:14PX;" >name: Created collection name options: is a document to initialize (optional) </span>
<span style= "FONT-SIZE:14PX;" >> db.createcollection ("Jingdong") #无参数 {"OK": 1}> show collectionsjingdongsystem.indexes> Db.createcollection ("Jingdong", {capped:1, autoindexid:1, size:6142800, max:10000}) #带参数 {"OK": 1}</sp An>
Capped: The type is Boolean, and if ture creates a fixed-size collection, the previous entry can be overwritten automatically when its entry reaches its maximum. The parameter size is also specified when setting it to ture;
AUTOINDEXID: The type is Boolean, the default is False, and if set to Ture, the index is automatically created on the _id Field.s;
Size: If capped is required for ture, specify the maximum value of the parameter, in bytes;
Max: Specifies the maximum number of documents. You can also not create a collection in Mogodb because the collection is created automatically when the document is created
Delete Collectiondb. Collection.drop ()
<span style= "FONT-SIZE:14PX;" >> use jdswitched to DB jd> show collectionsjingdongjingdong1system.indexes> db.jingdong.drop () ture> Show collectionsjingdong1system.indexes</span>
Insert Documentdb. Collection_name.insert (document)
</pre><pre name= "code" class= "python" ><span style= "FONT-SIZE:14PX;" >> 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 "}]}) > doc= ({" Name ":" Peter "," position ":" Teacher "}) #先定义文档 > Use jdswitched to DB jd> Db.jingdong.insert (Userdoc1) writeresult ({"ninserted": 1}) > Db.jingdong.insert (USERDOC2) writeresult ({" Ninserted ": 1}" > Db.jingdong.insert (Doc1) writeresult ({"ninserted": 1}) </span>
Update documentdb. Collection_name.update (Selection_criteria,updated_data)
<span style= "FONT-SIZE:14PX;" > #将user_id =2 's document e-mail to [email protected] > Db.jingdong.update ({"user_id": "The", "E-mail": "[email] Protected] "},{$set: {" E-mail ":" [email protected] "}) #第一个大括号内容标示查找条件, the second curly brace content represents the updated data Writeresult ({" nmatched ": 1," Nupserted ": 1," nmodified ": 1}) > Db.jingdong.find () </span>
<pre name= "code" class= "python" ><span style= "FONT-SIZE:14PX;" > The default update function updates only one document, and if you want to work on all documents, you need to join multi:ture</span>
<span style= "FONT-SIZE:14PX;" >db.shiyanlou.update ({"E-mail": "[email protected]"},{$set: {"E-mail": "[email protected]"}},{multi:ture}) </ Span>
Replace a document that already existsdb. Collection_name.save ({_id:objectid (), new_data})
<span style= "FONT-SIZE:14PX;" >> Db.shiyanlou.save ({"_id": ObjectId ("53ea174ccb4c62646d9544f4", "name": "Bob", "position": "Techer")}) Writeresult ({"nmatched": 1, "nupserted": 1, "nmodified": 1}) </span>
Delete a documentdb. Collection_name.remove (Delection_criteria)
<span style= "FONT-SIZE:14PX;" >> Db.shiyanlou.remove ({"Name": "Bob"}) Writeresult ({"nremoved": 1}) </span>
You can then use Find () to view all the documents and verify that the deletion was successful.
CreateCollection (): Create Collection
Db. Collection.drop (): Delete Collection
Db. Collection_name.insert (document): Inserting documents
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): Deleting documents
MongoDB Database Update and delete documents