一、查看數據庫javascript
查看mongodb中的數據庫(相似於mysql中的show databases);java
> show dbs local 0.000GB test 0.000GB
二、使用數據庫mysql
若是使用的數據庫不存在,mongodb會自動建立對應的數據庫(而mysql須要create database <數據庫名>)redis
> use testdb switched to db testdb
三、查看當前使用的數據庫(相似於MySQL中的 select database();)
sql
> db testdb
四、刪除數據庫mongodb
> use testdb switched to db testdb > db.dropDatabase() { "dropped" : "testdb", "ok" : 1 }
mongodb中是分爲集合和文檔的。相似於 MySQL中的表和行的關係shell
一、插入文檔(就是相似於插入MySQL表中的上數據,只是在mongodb中這裏不須要直接建立對應的所謂的表)數據庫
語法:db.<collection_name(集合名稱,"所謂的表名")>.insert({"<鍵名>":"<鍵值>"......})函數
由於mongodb是數據文檔型數據庫,這種{}的結構基本上和javascript中的對象,Python中的字典,redis中的散列是相似的。說白了就是映射關係spa
> db.collection1.insert({"name":"xiaoming"}) WriteResult({ "nInserted" : 1 }) > db.collection1.insert({"name":"xiaoming2"}) WriteResult({ "nInserted" : 1 }) > db.collection1.insert({"name":"xiaoming3","tel":"10086"}) WriteResult({ "nInserted" : 1 }) > db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaoming2" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086"
上面的是在集合collection1中插入的三個文檔,就是相似的鍵值對應鍵名相似mysql中insert into tablename (列名)value(值);
二、更新文檔
語法:db.<集合名>.update({"鍵值":"鍵名"},{$set:{"鍵值":"鍵名"}})前面是條件,後面是內容
相似於MySQL中 update collection1 set 列名=列值 where 列名=列值;
> db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaoming2" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } > db.collection1.update({"name":"xiaoming2"},{$set:{"name":"xiaohong"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection1.update({"name":"xiaoming"},{$set:{"tel":"123456"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" }
上面的例子能夠看出改一個不存在的記錄來對記錄進行新增的操做。
對多行記錄進行修改
> db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong1" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong1" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong1" } > db.collection1.update({"name":"xiaohong1"},{$set:{"name":"xiaohong2"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong1" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong1" } > db.collection1.update({"name":"xiaohong1"},{$set:{"name":"xiaohong2"}},false,true) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" } >
默認的update只對一行進行修改,須要在要修改的記錄後面加入參數 false,true
其實具體的含義就是 False找不到就進行插入操做,true進行修改多行
進行遞增和遞減的操做 $inc
> db.collection2.insert({"grade":60}) WriteResult({ "nInserted" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 60 } > db.collection2.update({"grade":60},{$inc:{"grade":2}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 62 } > db.collection2.update({"grade":62},{$inc:{"grade":-3}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59 }
刪除某一列使用參數 $unset
> db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676"), "name" : "xiaohong" } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" } > db.collection1.update({"name":"xiaohong"},{$unset:{"name":"xiaohong2"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676") } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" }
save是一個shell函數,在文檔中不存在時插入,存在時更新
語法:db.<集合名>.save()
> db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59 } > x.num=42 42 > db.collection2.save(x) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 42 } > x.num=43 43 > db.collection2.save(x) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.collection2.find() { "_id" : ObjectId("56e439c3cd979329c402b67b"), "grade" : 59, "num" : 43 }
上面x不存在時候save就進行插入,x存在時候進行更新的操做
三、刪除文檔
Db.<集合名>.remove({"鍵值":"鍵名"})(刪除的where語句。)
Db.<集合名>.drop()(刪除全部的行)
> db.collection1.find() { "_id" : ObjectId("56e42cffcd979329c402b675"), "name" : "xiaoming", "tel" : "123456" } { "_id" : ObjectId("56e42d25cd979329c402b676") } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" } > db.collection1.remove({"name":"xiaoming"}) WriteResult({ "nRemoved" : 1 }) > db.collection1.find() { "_id" : ObjectId("56e42d25cd979329c402b676") } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } { "_id" : ObjectId("56e43689cd979329c402b678"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368ccd979329c402b679"), "name" : "xiaohong2" } { "_id" : ObjectId("56e4368dcd979329c402b67a"), "name" : "xiaohong2" } > db.collection1.remove({"name":"xiaohong2"}) WriteResult({ "nRemoved" : 3 }) > db.collection1.find() { "_id" : ObjectId("56e42d25cd979329c402b676") } { "_id" : ObjectId("56e42d9dcd979329c402b677"), "name" : "xiaoming3", "tel" : "10086" } > db.collection1.drop() true
四、查詢操做
先說一下簡單的一些條件格式吧,有點多,後面再繼續寫。。。。
等於 {<key>:<value>
}
小於 {<key>:{$lt:<value>}}
小於或等於 {<key>:{$lte:<value>}}
大於
{<key>:{$gt:<value>}}
大於或等於
{<key>:{$gte:<value>}}
不等於
{<key>:{$ne:<value>}}
下一篇開寫吧