时间:2021-07-01 10:21:17 帮助过:25人阅读
<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":" test@qq.com","VM_num":2,"time":[{"date":"2014-08-12","hour":"10:53 PM"}] }) > userdoc2=({"user_id":2,"name":"testadmin","state":"active","actor":"admin","e-mail":" test@qq.com","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改为group@qq.com > db.jingdong.update({"user_id":"02","e-mail":"test@qq.com"},{$set:{"e-mail":"group@qq.com"}}) #第一个大括号内容标示查找条件,第二个大括号内容则表示更新后的数据 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":"test@qq.com"},{$set:{"e-mail":"group@qq.com"}},{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):删除文档
MongoDB数据库更新和删除文档
标签:mongodb 数据库 索引 数据