[mongo] 1.1 mongodb基本介紹
MongoDB是一個文檔數(shù)據(jù)庫,旨在簡化開發(fā)和擴展。該手冊介紹了MongoDB中的關(guān)鍵概念,介紹了查詢語言,并提供了操作和管理方面的考慮因素與過程以及全面的參考部分。
MongoDB提供數(shù)據(jù)庫的社區(qū)版和企業(yè)版:
MongoDB社區(qū)是MongoDB的可用源和免費版本。
MongoDB Enterprise作為MongoDB Enterprise Advanced訂閱的一部分提供,并且包括對MongoDB部署的全面支持。MongoDB Enterprise還添加了以企業(yè)為中心的功能,例如LDAP和Kerberos支持,磁盤上的加密以及審核。
文檔數(shù)據(jù)庫
MongoDB中的記錄是一個文檔,它是由field 和 value對組成的數(shù)據(jù)結(jié)構(gòu)。MongoDB文檔類似于JSON對象。字段的值可以包括其他文檔,arrays (數(shù)組)和arrays of documents(文檔數(shù)組)。
使用文檔的優(yōu)點是:
文檔(即對象)對應(yīng)于許多編程語言中的本機數(shù)據(jù)類型。
嵌入式文檔和數(shù)組減少了對高成本的連接需求。
動態(tài)模式支持流暢的多態(tài)性。
Collections/Views/On-Demand Materialized Views
MongoDB將文檔存儲在集合中。集合類似于關(guān)系數(shù)據(jù)庫中的表。
除集合外,MongoDB還支持:
Read-only?Views?(從MongoDB 3.4開始)
On-Demand Materialized Views(從MongoDB 4.2開始)。
關(guān)鍵特性
高性能
MongoDB提供高性能的數(shù)據(jù)持久性。特別是,
對嵌入式數(shù)據(jù)模型(embedded data models)的支持減少了數(shù)據(jù)庫系統(tǒng)上的I/O動作。
索引支持更快的查詢,并且可以包含來自嵌入式文檔和數(shù)組的鍵(embedded documents and arrays)。
豐富的查詢語言
MongoDB支持豐富的查詢語言以支持讀寫操作(CRUD)以及:
數(shù)據(jù)匯總
文本搜索和地理空間查詢。
另外:
SQL到MongoDB的映射圖
SQL到聚合的映射圖
了解MongoDB查詢語言的最新查詢語言功能:MongoDB.live 2020中 ? 的新增?功能。
高可用性
MongoDB的復(fù)制工具(稱為副本集 replica set)提供:
自動故障轉(zhuǎn)移
數(shù)據(jù)冗余。
副本集是一組保持相同的數(shù)據(jù)集,從而提供冗余和提高數(shù)據(jù)可用性的MongoDB服務(wù)器。
水平可伸縮性
MongoDB提供水平可伸縮性作為其核心?功能的一部分:
Sharding?在一組計算機集群中分布存儲數(shù)據(jù)。
從3.4開始,MongoDB支持基于shard鍵創(chuàng)建數(shù)據(jù)區(qū)域。在平衡集群中,MongoDB僅將區(qū)域覆蓋的讀寫定向到區(qū)域內(nèi)的那些分片。有更多信息,請參見Zones手冊頁。
支持多種存儲引擎
MongoDB支持多個存儲引擎:
WiredTiger存儲引擎(包括對靜態(tài)加密Encryption at Rest的支持?)
In-Memory 存儲引擎。
此外,MongoDB提供 pluggable 存儲引擎API,允許第三方為MongoDB開發(fā)存儲引擎。
MongoDB Shell中進行查詢的各種示例。
有關(guān)使用MongoDB驅(qū)動程序的示例,請參閱“其他示例”部分中的鏈接。
Within the?shell,?db?refers to your current database. Type?db?to display the current database.
copy
copied
db
The operation should return?test, which is the default database.
To switch databases, type?use?
copy
copied
use examples
You do not need to create the database before you switch. MongoDB creates the database when you first store data in that database (such as create the first collection in the database).
To verify that your database is now?examples, type?db?in the?shell?above.
copy
copied
db
MongoDB將文檔存儲在集合中。集合類似于關(guān)系數(shù)據(jù)庫中的表。如果不存在集合,則在您第一次為該集合存儲數(shù)據(jù)時,MongoDB會創(chuàng)建該集合。
以下示例使用該?db.collection.insertMany()方法將新?文檔插入到inventory?集合中。
copy
copied
db.inventory.insertMany([ { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] }, { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] }, { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] }, { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] }, { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] } ]); // MongoDB adds an _id field with an ObjectId value if the field is not present in the document
該操作返回一個包含確認(rèn)指示符的文檔和一個包含_id每個成功插入的文檔的數(shù)組的數(shù)組?。
要從集合中選擇文檔,可以使用?db.collection.find()方法。要選擇集合中的所有文檔,請將空文檔作為查詢過濾器文檔傳遞給該方法。
copy
copied
db.inventory.find({})
要格式化結(jié)果,請將追加? .pretty()到?find操作:
copy
copied
db.inventory.find({}).pretty()
NOTE
The example assumes that you have populated the?inventory?collection from the previous step.
對于相等匹配 (i.e.?
NOTE
The examples assume that you have populated the?inventory?collection.
status?field equals?"D":
copy
copied
db.inventory.find( { status: "D" } );
qty?field equals?0:
copy
copied
db.inventory.find( { qty: 0 } );
qty?field equals?0?and?status?field equals?"D":
copy
copied
db.inventory.find( { qty: 0, status: "D" } );
size.uom?field equals?"in":
copy
copied
db.inventory.find( { "size.uom": "in" } )
size?field equals the document?{?h:?14,?w:?21,?uom:?"cm"?}:
copy
copied
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
嵌入式文檔( embedded document) 上的相等匹配要求完全匹配,包括字段順序。
tags數(shù)組匹配存在"red" 元素:
copy
copied
db.inventory.find( { tags: "red" } )
如果該tags字段是字符串而不是數(shù)組,則查詢只是一個相等匹配。
tags字段與指定數(shù)組完全匹配的文檔,包括順序:
copy
copied
db.inventory.find( { tags: [ "red", "blank" ] } )
要指定要返回的字段,請將投影(projection)文檔傳遞給該?方法。
db.collection.find(
_id,?item, and the?status?fields 返回inventory集合所有文檔:
copy
copied
db.inventory.find( { }, { item: 1, status: 1 } );
You do not have to specify the?_id?field to return the field. It returns by default. To exclude the field, set it to?0?in the projection document. For example, copy and paste the following to return only the?item, and the?status?fields in the matching documents:
copy
copied
db.inventory.find( {}, { _id: 0, item: 1, status: 1 } );
Additional Examples
For additional examples, including MongoDB driver specific examples (Python, Java, Node.js, etc.), see:
Query Documents
Query on Embedded/Nested Documents
Query an Array
Query an Array of Embedded Documents
Project Fields to Return from Query
Query for Null or Missing Fields
Update Documents
Delete Documents
MongoDB 云數(shù)據(jù)庫 GaussDB(for Mongo) 數(shù)據(jù)庫
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。