走進MongoDB(一)

本文從如下幾個知識點進行介紹:javascript

1、mongodb簡介java

2、在windows操做系統上安裝、配置MongoDBsql

3、The mongo shell的簡單使用mongodb

4、MongoDB CRUD Operations(增刪改查)shell

 

 

1、mongodb簡介數據庫

Mongodb是一款開源的文檔數據庫。json

Document datasewindows

mongodb的一條數據記錄就是一個文檔,它是由鍵-值對組成的一個數據結構。Mongodb文檔和json對象很類似。一個鍵值可能會包含其它的文檔、數據或者是文檔數組。數組

{服務器

Name:」sue」,

Age:26,

Gruops:[「news」,」sports」]

}

Key features

High performance 高性能

支持嵌入式的數據模型 來下降 I/O 活動

索引支持更快速的查詢,而且能夠包含嵌入的文檔和數組。

Rich query language 豐富的查詢語言

Data aggregation

Text search and geospatial queries (地理空間查詢)

High availability 高可用性

稱爲replica set (複製集),Replication facility提供:

自動的失效備援

數據冗餘

Horizontal scalability 水平擴展或橫向擴展

橫向擴展是Mongodb核心功能的一部分:

經過機器集羣分區分配數據。

Mongodb3.4支持建立基於 shard key 的數據區域。在一套平衡集羣系統上,Mongodb直接在數據區域內部的分片上直接讀寫數據。

Support for multiple storage engines

WiredTiger Storage Engine

MMAPv1 Storage Engine

另外,MongoDB提供了可插拔的存儲引擎接口,容許第三方來開發存儲引擎

Databases and Collections

Mongodb collections(集合中)保存數據記錄

Databases

mongodb中,數據庫持有 了文檔(documents)的集合(collection),

使用數據庫:

use myDB

Create a Database

當你第一次給數據庫存數據的時候,也就建立了數據庫。例如:切換到一個不存在的數據庫,而後插入一條數據:

Use myNewDB

db.myNewCollection1.insertOne({x:1})      //建立了myNewDB數據庫,也建立了myNewCollection1集合。

Collections

Mongodbcollections中存儲documents。集合和關係數據庫的表是相似的。

Create a collection(隱式建立方式)

在第一次給一個集合存儲數據時,也就建立了此集合:

db.myNewCollection2.insertOne({x:1})

db.myNewCollection3.createIndex({y:1})

Explicit creation(直接建立)

db.createCollection() 可使用多種和collectiondocument相關的選項參數。

Document validation

默認,集合不會要求文檔必需要用於徹底一致的模式;也就是說在一個數據集合內、不須要使用相同的字段(fields)集合,不一樣的文檔上同一字段的數據類型也能夠沒必要相同。

Mongodb3.2開始,能夠在collection中設置文檔的驗證規則。

Modifying Document Structure

能夠任意的修改一個集合中的文檔結構。例如,添加新的字段、刪除字段、改變字段的數據類型等...

 

2、在windows操做系統上安裝MongoDB

支持和要求

version2.2以後再也不支持XP系統。要求使用win server 2008 r2win vista等更高版本的win操做系統。

安裝:.

msi傻瓜式安裝

設置mongodb環境:(設置數據庫的數據存放目錄)

C:\mongodb\server\3.4\mongod.exe --dbpath c:\data\db

執行成功db目錄下生成系統數據庫文件,默認端口:27017

啓動服務:(直接運行mongod程序便可)

Command prompt: C:\mongodb\server\3.4\mongod.exe

默認把安裝磁盤根目錄下data\db做爲數據庫文件路徑。若是設置的data目錄不在磁盤根目錄下,須要 --dbpath 指定data路徑。

鏈接mongodb服務器

執行mongo.exe 客戶端程序便可。

關閉服務

mongod終端鍵入:control+c

 

mongodb配置爲windows系統服務

1.建立數據目錄和日誌目錄,如:

Mkdir c:\data\db

Mkdir c:\data\log

2.建立配置文件,如:

建立C:\mongodb\server\3.4\mongod.cfg,指定systemLog.path  storage.dbpath

systemLog:

    destination: file

    path: D:\database\MongoDB\data\log\mongod.log

storage:

dbPath: D:\database\MongoDB\data\db

3.註冊mongodb爲系統爲服務

C:\mongodb\server\3.4\mongod.exe --config 「C:\mongodb\server\3.4\mongod.cfg」 --install

4.啓動、關閉服務

Net start/stop mongodb

 

 

3、The mongo shell

查看正在使用的數據庫:

db

默認返回test(默認的數據庫)

切換/建立數據庫:

Use <database>   (你能夠切換到一個不存在的數據庫,若是你插入了一條數據,數據庫即被建立)

Use myNewDatabase

Db.myCollection.insertOne( { x:1 } )

數據查詢

Db.mycollection.find()

多行操做

若命令(){}[] 沒有成對出現,則能夠換行繼續鍵入操做

使用tab鍵進行命令提示

如,db.mycollection.c<tab> 會列出全部以c開頭的可用函數。

退出mongo shell

quit() or 使用 <Ctrl-C> shortcut.

 

Mongo shell的配置

自定義命令提示符:(顯示命令的行號)

cmdCount=1

Prompt=function(){return (cmdCount++)+」>」}

Change the mongo shell batch size

DBQuery.shellBatchSize=10;

訪問mongo shell幫助信息

命令行幫助  mongo  --help

shell幫助(in the mongo shellhelp

Show dbs 查看全部數據庫

db.help() 數據庫對象的方法

db.insertOne 查看方法的實現代碼

Collection 幫助

show collections

db.collection.help()

db.collection.save 查看collection方法的實現

 

Cursor 幫助

Db.collection.find().help()

Db.collection.find().toArray 方法實現代碼

Wrapper object 幫助

 

Write scripts for the mongo shell

這裏支持在mongo shell 使用javascipt腳原本進行數操做

Opening new connections

mongo shell或者js文件中,初始化鏈接數據庫實例的Mongo()構造函數。

New Mongo()

New Mongo(<host>)

New Mongo(<host:port>)

例如:

Conn=new Mongo();

Db=conn.getDb(「myDatabase」);

或者:db=connect(「localhost:27010/myNewDatabase」);

Differences between interactive and scripted mongo

使用--eval選項來執行javascript

mongo test --eval 「print(db.getCollectionNames())」;

Execute a javascript file:

Mongo loclhost:27017/test myjsfile.js

Mongo <js file path>

能夠在mongo shell界面,使用load() 函數執行js文件:

Load(「myjstest.js」) 注:文件路徑使用 \\ / 分開,當前默認路徑爲data/db

 

Data types in th e mongo shel

Types

Date  (內部存儲爲64bit 毫秒數、unix紀元(1970開始))

Date() String形式返回當前日期  typeof->String

New Date()  返回日期對象  instanceof Date=true  typeof -> object

ISODate()    返回日期對象  instanceof Date=true  typeof -> object

ObjectId

Mongo shell提供了ObjectId()包裝類。new ObjectId 生成 一個新的id

NumberLong

Mongo shell默認把全部數字當作浮點型的值。提供包裝類:NumberLong()操做64bit整數。

NumberInt

使用NumberInt()構造函數來顯示的指定一個額32bit 整數。

NumberDecimalnew in v-3.4

NumberDecimal()顯示指定128-bit基於十進制小數的浮點數,用於解決金融、貨幣上的計算問題。

Equality and sort order

Check types in the mongo shell

Instanceof   return boolean

Typeof  直接返回字段的類型

 

4、MongoDB CRUD Operations

全部對單個documents的操做都是原子性的

Create operation

collection中插入一個新的documents

db.collection.insertOne()

Db.collection.inesrtMany()

Read operation

db.collection.find()

Update operation

db.collection.updateOne()

db.collection.updateManu()

db.collection.replaceOne()

Delete operation

db.collection.deleteOne()

db.collection.deleteMany()

Bulk write Opeation(同時執行多種操做)

bulkWrite() 支持方法:insertOne updateOne updateMany deleteOne deleteMany

SQL to MongoDB Mapping Chartsqlmongo比較)

Terminology and concepts

Read Concern/Isolationsqlmongo比較)

Read concern levels

Local (default)

 

 

Majority

 

 

Linearizable (v3.4開始支持)

相關文章
相關標籤/搜索