一、MongoDB简介:

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

1、MongoDB 下载

你可以在mongodb官网下载该安装包,地址为:https://www.mongodb.com/download-center#community。MonggoDB支持以下平台:

  • OS X 32-bit
  • OS X 64-bit
  • Linux 32-bit
  • Linux 64-bit
  • Windows 32-bit
  • Windows 64-bit
  • Solaris i86pc
  • Solaris 64

2、MongoDB 应用案例

下面列举一些公司MongoDB的实际应用:

  • Craiglist上使用MongoDB的存档数十亿条记录。
  • FourSquare,基于位置的社交网站,在Amazon EC2的服务器上使用MongoDB分享数据。
  • Shutterfly,以互联网为基础的社会和个人出版服务,使用MongoDB的各种持久性数据存储的要求。
  • bit.ly, 一个基于Web的网址缩短服务,使用MongoDB的存储自己的数据。
  • spike.com,一个MTV网络的联营公司, spike.com使用MongoDB的。
  • Intuit公司,一个为小企业和个人的软件和服务提供商,为小型企业使用MongoDB的跟踪用户的数据。
  • sourceforge.net,资源网站查找,创建和发布开源软件免费,使用MongoDB的后端存储。
  • etsy.com ,一个购买和出售手工制作物品网站,使用MongoDB。
  • 纽约时报,领先的在线新闻门户网站之一,使用MongoDB。
  • CERN,著名的粒子物理研究所,欧洲核子研究中心大型强子对撞机的数据使用MongoDB。

3、MongoDB安装

百度一下,有安装教程

二、MongoDB  基本概念

1、基本概念

• 文档(document)

      – 类似于JS中的对象,在MongoDB中每一条数据都是一个文档

• 集合(collection)

      – 集合就是一组文档,也就是集合是用来存放文档的

     – 集合中存储的文档可以是各种各样的,没有格式要求

• 多个文档组成集合,多个集合组成数据库

mongodb可靠性 mongodb知乎_数据库

2、基本概念解析:

mongodb可靠性 mongodb知乎_MongoDB_02

三、MongoDB  基本操作

1、登录MongoDB

C:\Users\wanli>mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("645cf85d-6786-4919-9f82-76e65c61a42d") }
MongoDB server version: 4.0.5
Server has startup warnings:
2019-01-16T11:14:58.334+0800 I CONTROL  [initandlisten]
2019-01-16T11:14:58.334+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-16T11:14:58.334+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-16T11:14:58.334+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

2、查看操作

show <option> 根据参数显示列表
          • dbs 显示数据库列表
           • collections 显示当前数据库的集合
          • profile 显示时间超过1毫秒的system.profile条目
          • log[name] 显示登录记忆的最后一段

例如:show dbs:查看数据库列表

> show dbs
admin     0.000GB
config    0.000GB
deptment  0.000GB
local     0.000GB
test      0.000GB
zhang     0.000GB

3、切换数据库

   use <database> :如果存在数据库则切换数据库,没有则创建一个新的数据库

> use deptment
switched to db deptment

4、插入数据

   db.dep.insert({deptId:'1',deptname:'软件部'}):往当前数据库(deptment)中的dep文档中添加一条数据

> db.dep.insert({deptId:'1',deptname:'软件部'})
WriteResult({ "nInserted" : 1 })

5、查询数据

  db.dep.find():查询dep文档里面所有数据

> db.dep.find()
{ "_id" : ObjectId("5c3ecbb5abd3b038f09078bb"), "deptId" : "1", "deptname" : "软件部" }
{ "_id" : ObjectId("5c3ecbe3abd3b038f09078bc"), "deptId" : "2", "deptname" : "财务部" }
{ "_id" : ObjectId("5c3ecbf1abd3b038f09078bd"), "deptId" : "3", "deptname" : "后勤部" }
{ "_id" : ObjectId("5c3ecbfdabd3b038f09078be"), "deptId" : "4", "deptname" : "客服部" }
{ "_id" : ObjectId("5c3ecc07abd3b038f09078bf"), "deptId" : "5", "deptname" : "制单部" }

  db.dep.findOne():查询符合条件的第一条数据

> db.dep.findOne()
{
        "_id" : ObjectId("5c3ecbb5abd3b038f09078bb"),
        "deptId" : "1",
        "deptname" : "软件部"
}

db.dep.find({deptname:'财务部'}):查询deptname等于‘财务部’的数据

> db.dep.find({deptname:'财务部'})
{ "_id" : ObjectId("5c3ecbe3abd3b038f09078bc"), "deptId" : "2", "deptname" : "财务部" }

6、删除数据:

   db.dep.remove({deptId:'3'}) :删除deptId等于3的数据

   db.dep.remove():删除dep文档里面所有数据

> db.dep.remove({deptId:'3'})
WriteResult({ "nRemoved" : 1 })

  7、修改数据:

 db.dep.update({deptname:'后勤部'},{$set:{deptname:'人事部'}}):把deptname等于‘后勤部’修改为deptname等于‘人事部’(只修改一条数据)

db.dep.update({deptname:'后勤部'},{$set:{deptname:'人事部'}},{multi:true}) :把dep文档里面所有deptname等于‘后勤部’修改为deptname等于‘人事部’

> db.dep.update({deptname:'后勤部'},{$set:{deptname:'人事部'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })