<span style="font-size:14px;">name:創建的集合名稱 options:是一個作為初始化的文檔(可選)</span>
<span style="font-size:14px;">> db.createCollection("jingdong") #無參數 { "ok" : 1 } > show collections jingdong system.indexes > db.createCollection("jingdong", { capped : 1, autoIndexID : 1, size : 6142800, max : 10000 } ) #帶參數 { "ok ": 1 }</span>
<span style="font-size:14px;">> use JD switched to db JD > show collections jingdong jingdong1 system.indexes > db.jingdong.drop() ture > show collections jingdong1 system.indexes</span>
</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 JD switched 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>
<span style="font-size:14px;">#將user_id=2的文檔的e-mail改為[email protected] > db.jingdong.update({"user_id":"02","e-mail":"[email protected]"},{$set:{"e-mail":"[email protected]"}}) #第一個大括號內容標示查找條件,第二個大括號內容則表示更新後的數據 WriteResult({"nMatched":1,"nUpserted":1,"nModified":1}) > db.jingdong.find()</span>
<pre name="code" class="python"><span style="font-size:14px;">默認的update函數只對一個文檔更新,如果想作用所有文檔,則需要加入multi:ture</span>
<span style="font-size:14px;">db.shiyanlou.update({"e-mail":"[email protected]"},{$set:{"e-mail":"[email protected]"}},{multi:ture})</span>
<span style="font-size:14px;">> db.shiyanlou.save({"_id":ObjectId("53ea174ccb4c62646d9544f4","name":"Bob","position":"techer")}) WriteResult({"nMatched":1,"nUpserted":1,"nModified":1})</span>
<span style="font-size:14px;">> db.shiyanlou.remove({"name":"Bob"}) WriteResult({"nRemoved":1})</span>
createCollection():創建集合
db.COLLECTION.drop():刪除集合
db.COLLECTION_NAME.insert(document):插入文檔
db.COLLECTION_NAME.update(SELECTION_CRITERIA,UPDATED_DATA):更新文檔
db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA}):替換已存在的文檔
db.COLLECTION_NAME.remove(DELECTION_CRITERIA):刪除文檔