mongo基本使用方法
mongo與關(guān)系型數(shù)據(jù)庫(kù)的概念對(duì)比,區(qū)分大小寫,_id為主鍵。
?
1.數(shù)據(jù)庫(kù)操作
>show dbs? ?#查看所有數(shù)據(jù)庫(kù)
>use dbname? ? #創(chuàng)建和切換數(shù)據(jù)庫(kù)(如果dbname存在則切換到該數(shù)據(jù)庫(kù),不存在則創(chuàng)建并切換到該數(shù)據(jù)庫(kù);新創(chuàng)建的數(shù)據(jù)庫(kù)不會(huì)顯示,需要插入記錄才會(huì)顯示)
>db.dropDatabase()? #刪除數(shù)據(jù)庫(kù)(需要先use進(jìn)入到需要?jiǎng)h除的數(shù)據(jù)庫(kù))
?
2.集合操作
>show collections或者show tables? #查看集合(需要先use進(jìn)入數(shù)據(jù)庫(kù))
>db.createCollection(集合名字,參數(shù))? ?#創(chuàng)建集合
>db.集合名.drop()? ?#刪除集合
?
3.文檔操作
>db.集合名.insert(文檔)? ?#插入文檔,如果集合在該數(shù)據(jù)庫(kù)中不存在會(huì)先創(chuàng)建集合
>db.col.insert({title: 'MongoDB教程', description: 'MongoDB是一個(gè)Nosql數(shù)據(jù)庫(kù)',by: '菜鳥教程',url: 'https://www.runoob.com/mongodb/mongodb-tutorial.html',tags: ['mongodb', 'database', 'NoSQL'],likes: 100 })>db.col.insert({title: 'MySQL教程', description: 'MySQL是一個(gè)sql數(shù)據(jù)庫(kù)',by: '菜鳥教程',url: 'https://www.runoob.com/mysql/mysql-tutorial.html',tags: ['MySQL', 'database', 'SQL'],likes: 200 })?
?
>db.集合名.find(查詢條件).pretty()? ?#查詢文檔,find是以非結(jié)構(gòu)化的形式顯示所有文檔,加上pretty()以結(jié)構(gòu)化的形式顯示
對(duì)應(yīng)的findOne方法返回第一個(gè)滿足條件的文檔
> db.col.find().pretty() {"_id" : ObjectId("5d2e94f240515ad8a4693240"),"title" : "MongoDB教程","description" : "MongoDB是一個(gè)Nosql數(shù)據(jù)庫(kù)","by" : "菜鳥教程","url" : "https://www.runoob.com/mongodb/mongodb-tutorial.html","tags" : ["mongodb","database","NoSQL"],"likes" : 100 } {"_id" : ObjectId("5d2e960d40515ad8a4693241"),"title" : "MySQL教程","description" : "MySQL是一個(gè)sql數(shù)據(jù)庫(kù)","by" : "菜鳥教程","url" : "https://www.runoob.com/mysql/mysql-tutorial.html","tags" : ["MySQL","database","SQL"],"likes" : 200 }?
?
>db.集合名.update(查詢條件,更新操作,{upsert: <boolean>,multi: <boolean>,writeConcern: <document>})? ? ?#更新文檔
- upsert?: 可選,如果不存在update的記錄是否插入為新記錄,true為插入,默認(rèn)是false不插入。
- multi?: 可選,默認(rèn)是false,只更新找到的第一條記錄,如果設(shè)置為true則將按條件查出來的所有記錄全部更新。
- writeConcern?:可選,拋出異常的級(jí)別。
?
>db.集合名.save(文檔)? ? ??#更新文檔?
根據(jù)_id進(jìn)行判斷,如果要save的記錄在原集合中存在,則按照內(nèi)容對(duì)已存在的文檔進(jìn)行更新,如果不存在則進(jìn)行插入。
> db.col.save({"_id" : ObjectId("5d2e960d40515ad8a4693241"),title: 'Python教程',description: 'Python是一種解釋型、面向?qū)ο蟆?dòng)態(tài)數(shù)據(jù)類型的高級(jí)程序設(shè)計(jì)語(yǔ)言',by: '菜鳥教程',url: 'https://www.runoob.com/python3/python3-tutorial.html',tags: ['Python', '開發(fā)', '編程'],likes: 1000})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })?
db.col.save({"_id" : ObjectId("5d2e960d40515ad8a4693250"),title: 'HTML',description: '是一種用于創(chuàng)建網(wǎng)頁(yè)的標(biāo)準(zhǔn)標(biāo)記語(yǔ)言',by: '菜鳥教程',url: 'https://www.runoob.com/html/html-tutorial.html',tags: ['HTML', '前端', '編程'],likes: 300 }) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0 })?
>db.集合名.remove(查詢條件,justOne,writeConcern?)? ?#刪除文檔?
- query?:(可選)刪除的文檔的條件。
- justOne?: (可選)如果設(shè)為 true 或 1,只刪除一個(gè)文檔,不設(shè)置該參數(shù)使用默認(rèn)值 false,刪除所有匹配條件的文檔。
- writeConcern?:(可選)拋出異常的級(jí)別。
db.col.remove({})刪除所有文檔
?
轉(zhuǎn)載于:https://www.cnblogs.com/Forever77/p/11199289.html
總結(jié)
以上是生活随笔為你收集整理的mongo基本使用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 属狗梦到狗咬自己会怎么样
- 下一篇: 梦到抓螃蟹和小龙虾是什么意思