1、啟動shell:(主要用crt 軟件的時候終端要選擇linux,否則不能退格鍵有時候出問題)
[root@saltstack mongodb]# mongo
> show dbs #查看數據庫
admin (empty)
local 0.078GB
modbtest 0.078GB
myinfo (empty)
test (empty)
xiaoluo 0.078GB
> use dbtest #使用數據庫,沒有的話會自動創建
switched to db dbtest
#創建一個字典文檔,是以key,value方式存儲的:
> test = {'id':1,'name':'xiaoming','job':'it'}
{ "id" : 1, "name" : "xiaoming", "job" : "it" }
> test
{ "id" : 1, "name" : "xiaoming", "job" : "it" }
#自動創建一個叫dbtest的表,然后往里面插入test這個字典:
> db.dbtest.insert(test)
WriteResult({ "nInserted" : 1 })
#或者可以手工插入數據:
> db.dbtest.insert({'id':2,'name':'xiaoli','job':'it'})
WriteResult({ "nInserted" : 1 })
#查看數據使用find的方法:
> db.dbtest.find()
{ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }
{ "_id" : ObjectId("5513379383aef55e0cc2d05d"), "id" : 2, "name" : "xiaoli", "job" : "it" }
> show tables;
dbtest
system.indexes
#根據條件查找
> db.dbtest.find({'id':1})
{ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }
#根據條件進行更新里面的選型
> db.dbtest.update({'id':2},{'id':2,'name':'xiaoluo','job':'it'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbtest.find({'id':2})
{ "_id" : ObjectId("5513379383aef55e0cc2d05d"), "id" : 2, "name" : "xiaoluo", "job" : "it" }
>
#根據條件進行刪除使用remove方法。刪除id=2的字典:
> db.dbtest.remove({'id':2})
WriteResult({ "nRemoved" : 1 })
> db.dbtest.find()
{ "_id" : ObjectId("5513375e83aef55e0cc2d05c"), "id" : 1, "name" : "xiaoming", "job" : "it" }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。