查詢現有數據庫
>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 collections system.indexes userInfo
9.根據條件刪除
db.[documentName].remove({})
刪除集合中的name等於xiaoming的記錄
db.userInfo.remove({name:"xiaoming"})