走进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开始支持)

相关文章
相关标签/搜索