MongoDB基础命令
MongoDB 入門(mén)命令
查看當(dāng)前數(shù)據(jù)庫(kù)
> show dbs admin 0.000GB config 0.000GB local 0.000GB >-- use databaseName 選庫(kù) > use test switched to db test >-- show tables/collections 查看當(dāng)前庫(kù)下的collection > show tables > show collections >?基礎(chǔ)操作
Mongodb的庫(kù)是隱式創(chuàng)建,你可以u(píng)se 一個(gè)不存在的庫(kù)然后在該庫(kù)下創(chuàng)建collection,即可創(chuàng)建庫(kù)
--創(chuàng)建collection --db.createCollection(‘collectionName’) > db.createCollection('test') { "ok" : 1 } > > show tables test >--collection允許隱式創(chuàng)建 --Db.collectionName.insert(document); > db.stu.insert({stu:'001',name:'xiaoming'}) WriteResult({ "nInserted" : 1 }) > show tables stu test-- 刪除collection db.collectionName.drop()-- 刪除database db.dropDatabase(); > db.dropDatabase() { "dropped" : "test", "ok" : 1 } >?增
插入數(shù)據(jù)
> db.stu.insert({sid:"10"}) WriteResult({ "nInserted" : 1 }) > db.stu.insert({sid:"11"}) WriteResult({ "nInserted" : 1 })?
> db.stu.find() { "_id" : ObjectId("5c0c8a0b31a9b3cbb9df1d4f"), "sid" : "10" } { "_id" : ObjectId("5c0c8ac731a9b3cbb9df1d50"), "sid" : "11" }?
添加數(shù)據(jù)時(shí)不添加任何主鍵,會(huì)制動(dòng)生成一個(gè)主鍵,主鍵不會(huì)像關(guān)系型數(shù)據(jù)庫(kù)那樣自動(dòng)遞增(為了分布式考慮),使用的是時(shí)間戳+機(jī)器編號(hào)+進(jìn)程編號(hào)+序列號(hào)來(lái)生成,保證每個(gè)id都是唯一的.id為5c0c8a0b31a9b3cbb9df1d4f,可以分解為
5c0c8a0b 31a9b3 cbb9 df1d4f (5c0c8a0b)表示時(shí)間戳, 31a9b3 表示機(jī)器號(hào), cbb9 表示進(jìn)程編號(hào), df1d4f 表示序列號(hào)
我們也可以手動(dòng)指定ID
> db.stu.insert({_id:"001","sid":"12"}) WriteResult({ "nInserted" : 1 }) > db.stu.find() { "_id" : ObjectId("5c0c8a0b31a9b3cbb9df1d4f"), "sid" : "10" } { "_id" : ObjectId("5c0c8ac731a9b3cbb9df1d50"), "sid" : "11" } { "_id" : "001", "sid" : "12" } >?批量插入
> db.user.insert([ ... {username:"xiaoming",nickname:"XM",passwd:"123"} , ... {username:"xiaogang",nickname:"XG",passwd:"111"}, ... {username:"xiaohua",nickname:"XH",passwd:"123"} ... ]) BulkWriteResult({"writeErrors" : [ ],"writeConcernErrors" : [ ],"nInserted" : 3,"nUpserted" : 0,"nMatched" : 0,"nModified" : 0,"nRemoved" : 0,"upserted" : [ ] }) # 查看數(shù)據(jù) > db.user.find().pretty() {"_id" : ObjectId("5c0c8f3531a9b3cbb9df1d51"),"username" : "xiaoming","nickname" : "XM","passwd" : "123" } {"_id" : ObjectId("5c0c8f3531a9b3cbb9df1d52"),"username" : "xiaogang","nickname" : "XG","passwd" : "111" } {"_id" : ObjectId("5c0c8f3531a9b3cbb9df1d53"),"username" : "xiaohua","nickname" : "XH","passwd" : "123" }?
執(zhí)行插入數(shù)據(jù)的時(shí)候,驅(qū)動(dòng)程序會(huì)把數(shù)據(jù)轉(zhuǎn)換成為BSON格式,然后將數(shù)據(jù)輸入數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)會(huì)解析BSON,并檢驗(yàn)是否含有“_id”鍵,因?yàn)橛脩羧绻麤](méi)有自定義”_id”,會(huì)自動(dòng)生成,而且每次插入的文檔不能超過(guò)16M(插入文檔的大小跟MongoDB版本有關(guān)系)
刪除文檔
方式一
db.user.deleteMany({})
> db.user.deleteMany({}) { "acknowledged" : true, "deletedCount" : 3 } > > db.user.remove({}) WriteResult({ "nRemoved" : 3 })?上述命令會(huì)刪除user所有的文檔,不刪除集合本身,原有的索引也會(huì)保留,remove函數(shù)可以接收一個(gè)查詢文檔作為可選參數(shù)給定參數(shù)后,可以刪除指定符條件的文檔。
方式二
> db.user.remove({passwd:"123"}) WriteResult({ "nRemoved" : 2 }) > db.user.find().pretty() {"_id" : ObjectId("5c0c91d131a9b3cbb9df1d5b"),"username" : "xiaogang","nickname" : "XG","passwd" : "111" }?
刪除數(shù)據(jù)是永久性的不可以撤銷(xiāo)也不能恢復(fù)。
更新文檔
在MongoDB中更新單個(gè)文檔的操作是原子性的,默認(rèn)情況下如果一個(gè)update操作多個(gè)文doc,那么對(duì)于每個(gè)doc的更新是原子性的,但是對(duì)于整個(gè)update操作而言,不是原子性的可能存在前面的doc更新成功,而后面的文檔更新失敗,由于更新單個(gè)文檔doc的操作是原子性的,如果兩個(gè)更新同時(shí)發(fā)生,那么一個(gè)更新操作會(huì)足協(xié)另外一個(gè),doc的最終結(jié)果的值是由事件靠后的更新操作刪除決定的.
格式
db.collection.update(critera,objNew,upset,multi)
critera:查詢條件
objNew:update對(duì)象和一些更新操作符
upset:如果存在update記錄,是否插入objNew這個(gè)新文檔,true為插入,默認(rèn)為false
multi:默認(rèn)是false,值更新找到的第一條記錄,如果是true,按照條件查詢出看來(lái)的記錄全部更新
save
另一個(gè)更新命令是save 格式如下
db.collection.save(object)
obj表示要更新的對(duì)象,如果內(nèi)部已經(jīng)存在一個(gè)和obj相同的”_id”的記錄紙Mongodb會(huì)把obj對(duì)象替換集合內(nèi)已存在的記錄,如果不存在,則會(huì)插入obj對(duì)象.
文檔替換
用于一個(gè)新文檔完全替代匹配的文檔,這種方法先把數(shù)據(jù)讀出來(lái),之后對(duì)對(duì)象的方式完成修改,這種方式一般用在修改較大的情況下:
db.user.insertOne({ name:"foo",nickname:"bar",friends:12,enemies:2 })?我們希望把數(shù)據(jù)修改成為
db.user.insertOne({ name:"foo",nickname:"bar",relations:{friends:12,enemies:2} })?
步驟
查詢對(duì)象存儲(chǔ)到u中:
var u = db.user.findOne({name:"foo"})?設(shè)置relations的值:
u.relations = {friends:u.friends,enemies:u.enemies}?修改username的值:
> u.username = u.name foo?刪除friends:
> u.username = u.name foo?刪除enmies:
> delete u.enemies true?刪除name:
delete u.name?替換對(duì)象
> db.user.update({name:"foo"},u) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })?查詢
> db.user.find().pretty() {"_id" : ObjectId("5c0cab14d22a51c6ef9cdcee"),"nickname" : "bar","relations" : {"friends" : undefined,"enemies" : undefined},"username" : "foo" }?
這種替換基于編程思想來(lái)進(jìn)行的這種方式對(duì)單個(gè)對(duì)象傅咋修改比較適用
使用修改器
修改文檔只修改文章的部分,而不是全部,這個(gè)時(shí)候我們可以使用修改器對(duì)文檔進(jìn)行更新,他的主要思想是通過(guò)$符號(hào)來(lái)進(jìn)行修改這些操作
增加和減少
inc可以對(duì)數(shù)據(jù)進(jìn)行增加和減少,這個(gè)操作只針對(duì)數(shù)字類型,小數(shù)或者整數(shù).
添加一條數(shù)據(jù):
> db.topic.insertOne({title:"first",version:108}) {"acknowledged" : true,"insertedId" : ObjectId("5c0cb1ba422725fda4bd5746") } > db.topic.find().pretty() {"_id" : ObjectId("5c0cb1ba422725fda4bd5746"),"title" : "first","version" : 108 }?將數(shù)字減少3
> db.topic.update({"title" : "first"},{$inc:{version:-3}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.topic.find().pretty() {"_id" : ObjectId("5c0cb1ba422725fda4bd5746"),"title" : "first","version" : 105 }?
$set修改器
使用 set 可以完成的頂?shù)男枨笮薷?/p> 原始數(shù)據(jù) > db.author.find().pretty() {"_id" : ObjectId("5c0cb444422725fda4bd5747"),"name" : "foo","age" : 20,"gender" : "male","intro" : "student" }
?將intro 修改為 teacher
> db.author.update({name:"foo"},{$set:{intro:"teacher"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.author.find().pretty() {"_id" : ObjectId("5c0cb444422725fda4bd5747"),"name" : "foo","age" : 20,"gender" : "male","intro" : "teacher" } >?
$push修改器
使用push可以完成數(shù)組的插入,會(huì)在最后一條插入,如果沒(méi)有這個(gè)key會(huì)自動(dòng)創(chuàng)建一長(zhǎng)條插入
> db.post.find().pretty() {"_id" : ObjectId("5c0cc527422725fda4bd5748"),"title" : "a blog","content" : "...","author" : "foo" } #s使用push插入數(shù)組db.post.update({title:"a blog"},{$push:{comments:{name:"lina",email:"lina@email.com",content:"lina replay"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.post.find().pretty() {"_id" : ObjectId("5c0cc527422725fda4bd5748"),"title" : "a blog","content" : "...","author" : "foo","comments" : [{"name" : "lina","email" : "lina@email.com","content" : "lina replay"}] }?
addToSet修改器
使用addToSet可以向一個(gè)數(shù)組添加元素,有一個(gè)限定條件,如果存在了就不添加.
{"_id" : ObjectId("5c0cc9f3cdb93a655448eec5"),"name" : "foo","age" : 12,"email" : ["foo@example.com","foo@163.com"] } ## 添加集合 > db.user.update({name:"foo"},{$addToSet:{email:"foo@qq.com"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 查詢結(jié)果 > db.user.find().pretty() {"_id" : ObjectId("5c0cc9f3cdb93a655448eec5"),"name" : "foo","age" : 12,"email" : ["foo@example.com","foo@163.com","foo@qq.com"] } > ## 插入一個(gè)存在的數(shù)據(jù) > db.user.update({name:"foo"},{$addToSet:{email:"foo@qq.com"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.user.find().pretty() {"_id" : ObjectId("5c0cc9f3cdb93a655448eec5"),"name" : "foo","age" : 12,"email" : ["foo@example.com","foo@163.com","foo@qq.com"] }?nModified鍵的值為 0 ,因?yàn)橐呀?jīng)添加了,所以執(zhí)行添加語(yǔ)句的時(shí)候不會(huì)重復(fù)添加的,這種機(jī)制減少了數(shù)據(jù)庫(kù)的冗余數(shù)據(jù).
更新多個(gè)文檔
> db.clllections.update({x:1},{x:99}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.clllections.find().pretty() { "_id" : ObjectId("5c0ccc58cdb93a655448eec6"), "x" : 99 } { "_id" : ObjectId("5c0ccc61cdb93a655448eec7"), "x" : 1 } { "_id" : ObjectId("5c0ccc65cdb93a655448eec8"), "x" : 1 } { "_id" : ObjectId("5c0ccc6acdb93a655448eec9"), "x" : 2 } > 只有第一條匹配了 采用如下命令 > db.clllections.update({x:1},{$set:{x:99}},false,true) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.clllections.find().pretty() { "_id" : ObjectId("5c0ccc58cdb93a655448eec6"), "x" : 99 } { "_id" : ObjectId("5c0ccc61cdb93a655448eec7"), "x" : 99 } { "_id" : ObjectId("5c0ccc65cdb93a655448eec8"), "x" : 99 } { "_id" : ObjectId("5c0ccc6acdb93a655448eec9"), "x" : 2 }首先我們要將修改的數(shù)據(jù)賦值給$set,$set是一個(gè)修改器,我們將在上文詳細(xì)講解過(guò),然后后面多了兩個(gè)參數(shù),第一個(gè)flase表示如果不存在update記錄,是否將我們要更新的新文檔插入,true 表示插入 false 表示不插入,第二個(gè)true表示是否更新全部屬性的文章,false表示值更新第一條記錄,true表示更新所有查到的文檔.
?
轉(zhuǎn)載于:https://www.cnblogs.com/fmgao-technology/p/10410775.html
總結(jié)
以上是生活随笔為你收集整理的MongoDB基础命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: hashCode()方法(覆盖hashC
- 下一篇: 数组实例的find()和findInde