標籤:insert 資料庫
查詢現有資料庫
>show dbs
2.建立資料庫,需要建立集合這個庫才建立
>use mydb
3.查看集合
>show collections或者>show tables
4.建立文檔並插入資料
>db.userInfo.insert({_id:1,name:"xiaoming"})
5.批量插入文檔,shell是不支援批量插入的,要想完成批量插入可以用mongodb的應用驅動或者shell的for迴圈
>for(var i =0;i<10;i++){..db.userInfo.insert({name:i})..}
6.查看文檔內容
>db.userInfo.find()
7.save操作,save操作和insert操作區別在於遇到_id相同的情況下
save完成儲存操作
insert則會報錯
> db.userInfo.insert({_id:1,name:"xiaoming"})WriteResult({ "nInserted" : 1 })> db.userInfo.insert({_id:1,name:"xiaocang"})WriteResult({"nInserted" : 0,"writeError" : {"code" : 11000,"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foobar.userInfo.$_id_ dup key: { : 1.0 }"}})> db.userInfo.save({_id:1,name:"xiaocang"})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
8.刪除列表中所有資料,集合和索引不會被刪除
> db.userInfo.remove({})WriteResult({ "nRemoved" : 1 })> db.userInfo.find()> show collectionssystem.indexesuserInfo
9.根據條件刪除
db.[documentName].remove({})
刪除集合中的name等於xiaoming的記錄
db.userInfo.remove({name:"xiaoming"})
本文出自 “每天進步一點點” 部落格,請務必保留此出處http://08jw3.blog.51cto.com/998816/1675975
mongodb筆記1(基本操作,增刪改)