MonogoDb学习笔记
生活随笔
收集整理的這篇文章主要介紹了
MonogoDb学习笔记
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近重新學(xué)習(xí)了Mongodb,總結(jié)下了Monogodb的用法,以便以后查看。
備份:mongodump -h 127.0.0.1 -d spm -o /home/liuwei 還原:mongorestore -h dbhost -d dbname -directoryperdb /home/liuwei/spm 顯示所有數(shù)據(jù)庫(kù):show dbs; 創(chuàng)建數(shù)據(jù)庫(kù): 如果數(shù)據(jù)庫(kù)不存在,則創(chuàng)建數(shù)據(jù)庫(kù),否則切換到指定數(shù)據(jù)庫(kù)。use dbname; 刪除數(shù)據(jù)庫(kù):db.dropDatabase(); 創(chuàng)建集合: capped 是否固定集合 配合size autoindexid 自動(dòng)為_(kāi)id添加索引 max集合中包含文檔的最大數(shù)量db.createCollection('student');db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) 刪除集合:db.student.drop(); 顯示所有集合 show collections; 插入文檔:db.student.insert({code:"201517020119",name:"zhangsan",age:20}); 刪除文檔:db.student.remove({code:"201517020119"}); 更新文檔: db.update({條件},{更改的值key:value},如果不存在是否新建(true|false-默認(rèn)),只找匹配的第一條記錄-默認(rèn)false);$incdb.student.update({"code":"111"},{"$inc":{"age":1}});$set 沒(méi)有就新建db.student.update({"code":"111"},{"$set":{"score":100}})$unset 刪除字段db.student.update({"code":"112"},{"$unset":{"score":11}}) $rename { $rename : { old_field_name : new_field_name } }$push 往集合的數(shù)組中插入數(shù)據(jù)db.student.update({"code":"111"},{"$push":{"books":"數(shù)據(jù)結(jié)構(gòu)"}})$pushAlldb.student.update({"code":"111"},{"$pushAll":{"books":["數(shù)據(jù)結(jié)構(gòu)","語(yǔ)文"]}})$addToSet 往集合的數(shù)組中插入數(shù)據(jù) --避免重復(fù)db.student.update({"code":"111"},{"$addToSet":{"books":["數(shù)據(jù)結(jié)構(gòu)"]}})$pop 刪除制定位置的元素 -1:頭部 1:末尾db.student.update({"code":"111"},{"$pop":{"books":-1}})$pull 刪除指定名稱(chēng)的數(shù)據(jù) db.student.update({"code":"111"},{"$pull":{"books":"數(shù)據(jù)結(jié)構(gòu)"}})$gt > $gte >= $lt < $lte <= $ne !=$in db.student.find({"code":{"$in":["111","112"]}}); $nin db.student.find({"code":{"$nin":["111","112"]}}); $or db.student.find({"$or":[{"code":"111"},{"code":"112"}]}); $mod [n,m] *%n==m db.student.find({"age":{"$mod":[10,7]}}); $notdb.student.find({"age":{"$not":{"$mod":[10,7]}}});正則查詢(xún) i表示忽略大小寫(xiě)db.student.find({"name":/z/i});$all 滿足數(shù)組所有元素db.student.find({"books":{"$all":["數(shù)據(jù)結(jié)構(gòu)","語(yǔ)文"]}}); $elemMatch 匹配數(shù)組的內(nèi)嵌文檔 db.student.find({"books":{"$elemMatch":{"name":"語(yǔ)文","auth":"zhangsan"}}})$size 數(shù)組的大小db.student.find({"books":{"$size":2}});$slice 返回?cái)?shù)組指定記錄條數(shù) [10,3] 返回11~13 -1從后計(jì)算db.student.find({},{"books":{"$slice":1}}).pretty();游標(biāo):var students = db.student.find();while(students.hasNext()){print(students.next().code);}sort() 1從小到大 -1 從大到小db.student.find().sort("code":1);索引:①創(chuàng)建索引 1按照升序 -1 降序db.student.ensureIndex({"code":1});db.student.ensureIndex({"code":1},{"unique":true}); //唯一索引db.student.ensureIndex({"code":1},{"dropDups",true}); Boolean 在建立唯一索引時(shí)是否刪除重復(fù)記錄,指定 true 創(chuàng)建唯一索引。默認(rèn)值為 false.db.student.ensureIndex({"name":1},{"name":"ind_name"}); //制定名稱(chēng)②刪除索引 -- db.student.dropIndex( "indexName" ) or db.student.dropIndex( { "indexKey" : 1 } )db.student.dropIndex(name); db.studeng.dropIndexs(); // 刪除全部索引 數(shù)量查詢(xún) .count() db.student.count({"code":"111"});查詢(xún)分頁(yè): db.student.find().limit(2),skip(1); 去除重復(fù)數(shù)據(jù) distantdb.student.distinct("code") --> [112,113] aggregate一、group1、按照code分組 在返回每一組的總個(gè)數(shù)db.student.aggregate([{$group : {_id : "$code", "age_count" : {"$sum" : 1}}}])2、按照code分組 在返回每一組中age的總和db.student.aggregate([{$group : {_id : "$code", "age_sum" : {"$sum" : "$age"}}}])3、按照code分組 在返回每一組中age的總和 并升序排序 db.student.aggregate([{$group : {_id : "$code", "age_max" : {"$max" : "$age"}}},{$sort: {"_id": 1}}])總結(jié):$sum $max $min $avg $first $lastetc:$project:修改輸入文檔的結(jié)構(gòu)。可以用來(lái)重命名、增加或刪除域,也可以用于創(chuàng)建計(jì)算結(jié)果以及嵌套文檔。db.article.aggregate( { $project : { "code" : 1 , "name" : 1}});$match:用于過(guò)濾數(shù)據(jù),只輸出符合條件的文檔。$match使用MongoDB的標(biāo)準(zhǔn)查詢(xún)操作。db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: null, count: { $sum: 1 } } }] );$limit:用來(lái)限制MongoDB聚合管道返回的文檔數(shù)。$skip:在聚合管道中跳過(guò)指定數(shù)量的文檔,并返回余下的文檔。db.article.aggregate( { $skip : 5 });$unwind:將文檔中的某一個(gè)數(shù)組類(lèi)型字段拆分成多條,每條包含數(shù)組中的一個(gè)值。$group:將集合中的文檔分組,可用于統(tǒng)計(jì)結(jié)果。$sort:將輸入文檔排序后輸出。$geoNear:輸出接近某一地理位置的有序文檔按日、按月、按年、按周、按小時(shí)、按分鐘聚合操作如下:db.getCollection('m_msg_tb').aggregate( [{$match:{m_id:10001,mark_time:{$gt:new Date(2017,8,0)}}},{$group: {_id: {$dayOfMonth:'$mark_time'},pv: {$sum: 1}}},{$sort: {"_id": 1}} ]) $dayOfYear: 返回該日期是這一年的第幾天(全年 366 天)。 $dayOfMonth: 返回該日期是這一個(gè)月的第幾天(1到31)。 $dayOfWeek: 返回的是這個(gè)周的星期幾(1:星期日,7:星期六)。 $year: 返回該日期的年份部分。 $month: 返回該日期的月份部分( 1 到 12)。 $week: 返回該日期是所在年的第幾個(gè)星期( 0 到 53)。 $hour: 返回該日期的小時(shí)部分。 $minute: 返回該日期的分鐘部分。 $second: 返回該日期的秒部分(以0到59之間的數(shù)字形式返回日期的第二部分,但可以是60來(lái)計(jì)算閏秒)。 $millisecond:返回該日期的毫秒部分( 0 到 999)。 $dateToString: { $dateToString: { format: , date: } }。文檔引用:{"_id":ObjectId("52ffc33cd85242f436000001"), "address_ids": [ObjectId("52ffc4a5d85242602e000000"),ObjectId("52ffc4a5d85242602e000001")]} var result = db.users.findOne({"name":"Tom Benzamin"},{"address_ids":1}) var addresses = db.address.find({"_id":{"$in":result["address_ids"]}})explain()indexOnly: 字段為 true ,表示我們使用了索引。cursor:因?yàn)檫@個(gè)查詢(xún)使用了索引,MongoDB 中索引存儲(chǔ)在B樹(shù)結(jié)構(gòu)中,所以這是也使用了 BtreeCursor 類(lèi)型的游標(biāo)。如果沒(méi)有使用索引,游標(biāo)的類(lèi)型是 BasicCursor。這個(gè)鍵還會(huì)給出你所使用的索引的名稱(chēng),你通過(guò)這個(gè)名稱(chēng)可以查看當(dāng)前數(shù)據(jù)庫(kù)下的system.indexes集合(系統(tǒng)自動(dòng)創(chuàng)建,由于存儲(chǔ)索引信息,這個(gè)稍微會(huì)提到)來(lái)得到索引的詳細(xì)信息。n:當(dāng)前查詢(xún)返回的文檔數(shù)量。nscanned/nscannedObjects:表明當(dāng)前這次查詢(xún)一共掃描了集合中多少個(gè)文檔,我們的目的是,讓這個(gè)數(shù)值和返回文檔的數(shù)量越接近越好。millis:當(dāng)前查詢(xún)所需時(shí)間,毫秒數(shù)。indexBounds:當(dāng)前查詢(xún)具體使用的索引。還在繼續(xù)完善。。。
轉(zhuǎn)載于:https://www.cnblogs.com/LIUWEI123/p/9350216.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的MonogoDb学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU-3507Print Articl
- 下一篇: 事务管理 异常机制