摘要:是的簡(jiǎn)稱,是一個(gè)文檔對(duì)象的二進(jìn)制編碼格式。有以下三個(gè)特點(diǎn)輕量級(jí)跨平臺(tái)效率高命名空間存儲(chǔ)對(duì)象到這一系列的數(shù)據(jù)庫名和名被稱為一個(gè)命名空間。
mongodb由C++寫就,其名字來自humongous這個(gè)單詞的中間部分,從名字可見其野心所在就是海量數(shù)據(jù)的處理。關(guān)于它的一個(gè)最簡(jiǎn)潔描述為:scalable, high-performance, open source, schema-free, document-oriented database。MongoDB的主要目標(biāo)是在鍵/值存儲(chǔ)方式(提供了高性能和高度伸縮性)以及傳統(tǒng)的RDBMS系統(tǒng)(豐富的功能)架起一座橋梁,集兩者的優(yōu)勢(shì)于一身。 一個(gè)mongod服務(wù)可以有建立多個(gè)數(shù)據(jù)庫,每個(gè)數(shù)據(jù)庫可以有多張表,這里的表名叫collection,每個(gè)collection可以存放多個(gè)文檔(document),每個(gè)文檔都以BSON(binary json)的形式存放于硬盤中,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。它是以單文檔為單位存儲(chǔ)的,你可以任意給一個(gè)或一批文檔新增或刪除字段,而不會(huì)對(duì)其它文檔造成影響,這就是所謂的schema-free,這也是文檔型數(shù)據(jù)庫最主要的優(yōu)點(diǎn)。跟一般的key-value數(shù)據(jù)庫不一樣的是,它的value中存儲(chǔ)了結(jié)構(gòu)信息,所以你又可以像關(guān)系型數(shù)據(jù)庫那樣對(duì)某些域進(jìn)行讀寫、統(tǒng)計(jì)等操作。Mongo最大的特點(diǎn)是他支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對(duì)數(shù)據(jù)建立索引。Mongo還可以解決海量數(shù)據(jù)的查詢效率,根據(jù)官方文檔,當(dāng)數(shù)據(jù)量達(dá)到50GB以上數(shù)據(jù)時(shí),Mongo數(shù)據(jù)庫訪問速度是MySQL10 倍以上。 BSON是Binary JSON 的簡(jiǎn)稱,是一個(gè)JSON文檔對(duì)象的二進(jìn)制編碼格式。BSON同JSON一樣支持往其它文檔對(duì)象和數(shù)組中再插入文檔對(duì)象和數(shù)組,同時(shí)擴(kuò)展了JSON的數(shù)據(jù)類型。如:BSON有Date類型和BinDate類型。 BSON被比作二進(jìn)制的交換格式,如同Protocol Buffers,但BSON比它更“schema-less”,非常好的靈活性但空間占用稍微大一點(diǎn)。
BSON有以下三個(gè)特點(diǎn):
1. 輕量級(jí)
2. 跨平臺(tái)
3. 效率高
命名空間
MongoDB存儲(chǔ)BSON對(duì)象到collections,這一系列的數(shù)據(jù)庫名和collection名被稱為一個(gè)命名空間。
索引
mongodb可以對(duì)某個(gè)字段建立索引,可以建立組合索引、唯一索引,也可以刪除索引,建立索引就意味著增加空間開銷。默認(rèn)情況下每個(gè)表都會(huì)有一個(gè)唯一索引:_id,如果插入數(shù)據(jù)時(shí)沒有指定_id,服務(wù)會(huì)自動(dòng)生成一個(gè)_id,為了充分利用已有索引,減少空間開銷,最好是自己指定一個(gè)unique的key為_id,通常用對(duì)象的ID比較合適,比如商品的ID。
shell操作數(shù)據(jù)庫:
超級(jí)用戶相關(guān):
進(jìn)入數(shù)據(jù)庫admin
use admin
增加或修改用戶密碼
db.addUser("name","pwd")
查看用戶列表
db.system.users.find()
用戶認(rèn)證
db.auth("name","pwd")
刪除用戶
db.removeUser("name")
查看所有用戶
show users
查看所有數(shù)據(jù)庫
show dbs
查看所有的collection
show collections
查看各collection的狀態(tài)
db.printCollectionStats()
查看主從復(fù)制狀態(tài)
db.printReplicationInfo()
修復(fù)數(shù)據(jù)庫
db.repairDatabase()
設(shè)置記錄profiling,0=off 1=slow 2=all
db.setProfilingLevel(1)
查看profiling
show profile
拷貝數(shù)據(jù)庫
db.copyDatabase("mail_addr","mail_addr_tmp")
刪除collection
db.mail_addr.drop()
刪除當(dāng)前的數(shù)據(jù)庫
db.dropDatabase()
增刪改
存儲(chǔ)嵌套的對(duì)象
db.foo.save({"name":"ysz","address":{"city":"beijing","post":100096},"phone":[138,139]})
存儲(chǔ)數(shù)組對(duì)象
db.user_addr.save({"Uid":"yushunzhi@sohu.com","Al":["test-1@sohu.com","test-2@sohu.com"]})
根據(jù)query條件修改,如果不存在則插入,允許修改多條記錄
db.foo.update({"yy":5},{"$set":{"xx":2}},upsert=true,multi=true)
刪除yy=5的記錄
db.foo.remove({"yy":5})
刪除所有的記錄
db.foo.remove()
索引
增加索引:1(ascending),-1(descending)
db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
索引子對(duì)象
db.user_addr.ensureIndex({"Al.Em": 1})
查看索引信息
db.foo.getIndexes()
db.foo.getIndexKeys()
根據(jù)索引名刪除索引
db.user_addr.dropIndex("Al.Em_1")
查詢
查找所有
db.foo.find()
查找一條記錄
db.foo.findOne()
根據(jù)條件檢索10條記錄
db.foo.find({"msg":"Hello 1"}).limit(10)
sort排序
db.deliver_status.find({"From":"ixigua@sina.com"}).sort({"Dt",-1})
db.deliver_status.find().sort({"Ct":-1}).limit(1)
count操作
db.user_addr.count()
distinct操作,查詢指定列,去重復(fù)
db.foo.distinct("msg")
”>=”操作
db.foo.find({"timestamp": {"$gte" : 2}})
子對(duì)象的查找
db.foo.find({"address.city":"beijing"})
管理
查看collection數(shù)據(jù)的大小
db.deliver_status.dataSize()
查看colleciont狀態(tài)
db.deliver_status.stats()
查詢所有索引的大小
db.deliver_status.totalIndexSize()
advanced queries:高級(jí)查詢
條件操作符
gt:> lt : <
gte:>= lte: <=
ne:!=、<> in : in
nin:notin all: all
$not: 反匹配(1.3.3及以上版本)
查詢 name <> "bruce" and age >= 18 的數(shù)據(jù)db.users.find({name: {ne: "bruce"}, age: {gte: 18}});
查詢 creation_date > "2010-01-01" and creation_date <= "2010-12-31" 的數(shù)據(jù)db.users.find({creation_date:{gt:newDate(2010,0,1), lte:new Date(2010,11,31)});
查詢 age in (20,22,24,26) 的數(shù)據(jù)db.users.find({age: {$in: [20,22,24,26]}});
查詢 age取模10等于0 的數(shù)據(jù)db.users.find("this.age % 10 == 0");
或者
db.users.find({age : {$mod : [10, 0]}});
db.users.find({favorite_number : {$all : [6, 8]}});
可以查詢出
{name: "David", age: 26, favorite_number: [ 6, 8, 9 ] }
可以不查詢出{name: "David", age: 26, favorite_number: [ 6, 7, 9 ] }
db.users.find({name: {not: /^B.*/}});
查詢 age取模10不等于0 的數(shù)據(jù)db.users.find({age : {not: {$mod : [10, 0]}}});
返回部分字段選擇返回age和_id字段(_id字段總是會(huì)被返回)
db.users.find({}, {age:1});
db.users.find({}, {age:3});
db.users.find({}, {age:true});
db.users.find({ name : "bruce" }, {age:1});
0為false, 非0為true
db.users.find({ name : "bruce" }, {age:1, address:1});
排除返回age、address和_id字段db.users.find({}, {age:0, address:false});
db.users.find({ name : "bruce" }, {age:0, address:false});
數(shù)組元素個(gè)數(shù)判斷
對(duì)于{name: "David", age: 26, favorite_number: [ 6, 7, 9 ] }記錄
db.users.find({favorite_number: {size: 3}});
不匹配db.users.find({favorite_number: {size: 2}});
exists判斷字段是否存在
查詢所有存在name字段的記錄
db.users.find({name: {exists: true}});
查詢所有不存在phone字段的記錄
db.users.find({phone: {$exists: false}});
type判斷字段類型
查詢所有name字段是字符類型的
db.users.find({name: {type: 2}});
查詢所有age字段是整型的
db.users.find({age: {$type: 16}});
對(duì)于字符字段,可以使用正則表達(dá)式
查詢以字母b或者B帶頭的所有記錄
db.users.find({name: /^b.*/i});
$elemMatch(1.3.1及以上版本)
為數(shù)組的字段中匹配其中某個(gè)元素
Javascript查詢和where查詢
查詢 age > 18 的記錄,以下查詢都一樣
db.users.find({age: {gt: 18}});
db.users.find({$where: "this.age > 18"});
db.users.find("this.age > 18");
f = function() {return this.age > 18} db.users.find(f);
排序sort()
db.users.find().sort({age: 1}); #以年齡升序asc
db.users.find().sort({age: -1}); #以年齡降序desc
限制返回記錄數(shù)量limit()
db.users.find().limit(5); #返回5條記錄
db.users.find().limit(3).forEach(function(user) {print("my age is " + user.age)}); #返回3條記錄并打印信息
結(jié)果my age is 18
my age is 19
my age is 20
限制返回記錄的開始點(diǎn)skip()
從第3條記錄開始,返回5條記錄(limit 3, 5)db.users.find().skip(3).limit(5);
查詢記錄條數(shù)count()
db.users.find().count();
db.users.find({age:18}).count();
db.users.find().skip(10).limit(5).count();
如果要返回限制之后的記錄數(shù)量,要使用count(true)或者count(非0)db.users.find().skip(10).limit(5).count(true);
分組group()
假設(shè)test表只有以下一條數(shù)據(jù)
{ domain: "www.mongodb.org" , invoked_at: {d:"2009-11-03", t:"17:14:05"} , response_time: 0.05 , http_action: "GET /display/DOCS/Aggregation" } #使用group統(tǒng)計(jì)test表11月份的數(shù)據(jù)count:count(*)、 total_time:sum(response_time)、avg_time:total_time/count; db.test.group({ cond: {"invoked_at.d": {gt:"2009?11", lt: "2009-12"}} , key: {http_action: true}, initial: {count: 0, total_time:0} , reduce: function(doc, out){ out.count++; out.total_time+=doc.response_time } , finalize: function(out){ out.avg_time = out.total_time / out.count } } ); [{ "http_action" : "GET /display/DOCS/Aggregation", "count" : 1, "total_time" : 0.05, "avg_time" : 0.05 }]
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/18704.html
摘要:二中常用命令顯示數(shù)據(jù)庫列表顯示當(dāng)前數(shù)據(jù)庫中的集合類似關(guān)系數(shù)據(jù)庫中的表顯示用戶切換當(dāng)前數(shù)據(jù)庫,如果數(shù)據(jù)庫不存在則創(chuàng)建數(shù)據(jù)庫。注意操作同時(shí)可以創(chuàng)建數(shù)據(jù)庫,如果一個(gè)不存在的數(shù)據(jù)庫名,則執(zhí)行后,會(huì)創(chuàng)建對(duì)應(yīng)數(shù)據(jù)庫。如果指定字段,則會(huì)更新該的數(shù)據(jù)。 ..............................................................................
摘要:還原導(dǎo)出的表數(shù)據(jù)部分字段的表數(shù)據(jù)導(dǎo)入還原文件 一、 mongodump備份數(shù)據(jù)庫 1.一般常用的備份命令格式 mongodump -h IP --port 端口 -u 用戶名 -p 密碼 -d 數(shù)據(jù)庫 -o 文件存在路徑 如果想導(dǎo)出所有數(shù)據(jù)庫,可以去掉-d 2.導(dǎo)出數(shù)據(jù)庫[root@local ~]# mongodump -h 127.0.0.1 --port 30216 -d t...
摘要:?jiǎn)?dòng)允許后臺(tái)運(yùn)行,但是必須指定日志記錄文件路徑指定日志記錄文件路徑導(dǎo)出指定數(shù)據(jù)庫指定指定輸出文件名稱導(dǎo)出為格式指明需要導(dǎo)出哪些列指明導(dǎo)出格式為導(dǎo)入導(dǎo)入文件指明要導(dǎo)入的文件格式 啟動(dòng)MongoDB $mongod --fork --logpath=/data/log/r3.log--fork 允許mongod后臺(tái)運(yùn)行,但是必須指定日志記錄文件路徑(Enables a daemon mod...
摘要:常用命令查看當(dāng)前數(shù)據(jù)庫查看所有數(shù)據(jù)庫連接到數(shù)據(jù)庫查看當(dāng)前數(shù)據(jù)庫下所有的表查看表里的數(shù)據(jù)刪除當(dāng)前數(shù)據(jù)庫報(bào)錯(cuò)原因因?yàn)橹辽僖缘臄?shù)量進(jìn)行增長(zhǎng)當(dāng)磁盤空間不足時(shí)會(huì)報(bào)錯(cuò)解決方案在啟動(dòng)時(shí)加上參數(shù)例如或 MongoDB常用命令: db show dbs use xxx show collections db.yyy.find() db.dropDatabase() 報(bào)錯(cuò)Insufficient free...
摘要:既是數(shù)據(jù)庫,又是內(nèi)存數(shù)據(jù)庫,而且它是現(xiàn)在最強(qiáng)大最流行的數(shù)據(jù)庫。所以相對(duì)于的真內(nèi)存數(shù)據(jù)庫而言,只是將大部分的操作數(shù)據(jù)存在內(nèi)存中。 MongoDB既是NoSQL數(shù)據(jù)庫,又是內(nèi)存數(shù)據(jù)庫,而且它是現(xiàn)在最強(qiáng)大、最流行的NoSQL數(shù)據(jù)庫。區(qū)別與別的NoSQL數(shù)據(jù)庫,MongoDB主要是基于Documents文檔(即一條JSON數(shù)據(jù))的。 MongoDB的特點(diǎn): NoSQL數(shù)據(jù)庫 內(nèi)存數(shù)據(jù)庫 存儲(chǔ)...
摘要:如已存在數(shù)據(jù),再次進(jìn)行插入操作時(shí),會(huì)報(bào)主鍵重復(fù)的錯(cuò)誤提示會(huì)把修改為。使用函數(shù),參數(shù)指定查詢條件,不指定條件則查詢?nèi)坑涗?。年齡為或者名字為,年齡在中,。 基本命令 顯示當(dāng)前數(shù)據(jù)庫服務(wù)器上的數(shù)據(jù)庫show dbs 切換到指定數(shù)據(jù)庫的上下文,可以在此上下文中管理testdb數(shù)據(jù)庫以及其中的集合等use testdb 顯示數(shù)據(jù)庫中所有的集合(collection)show collecti...
閱讀 1710·2023-04-25 20:36
閱讀 2199·2021-09-02 15:11
閱讀 1273·2021-08-27 13:13
閱讀 2698·2019-08-30 15:52
閱讀 5419·2019-08-29 17:13
閱讀 1058·2019-08-29 11:09
閱讀 1537·2019-08-26 11:51
閱讀 901·2019-08-26 10:56