非关系型数据库(NoSql)--MongoDB

MongoDB数据库是除了redis之外的另外一个使用比较广泛的非关系数据库,他是基于分布式文件存储的开源数据库系统,旨在为web应用提供可扩展的高性能数据存储解决方案,将数据存储为一个文档,文档类似与Json格式。
{
name:'zhangsan',
age:18,
address:{city:'beijing',county:'china'}
}

MongoDB数据类型比较:

MongoDB数据库的操作:
一、MongoDB数据库的进入退出

C:\Users\Administrator>mongo #mongodb进入
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1852359e-6ed6-403b-95b9-1779c4cc4de6") }
MongoDB server version: 4.0.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten]
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-04-24T20:08:43.128-0700 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()
---

> exit #mongodb退出
bye

C:\Users\Administrator>mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8787bb6e-0845-44f0-8b01-7ace28b7ac8b") }
MongoDB server version: 4.0.9
Server has startup warnings:
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten]
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-24T20:08:43.127-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-04-24T20:08:43.128-0700 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()
---
>

二、库,集合的操作

操作状态 操作指令
显示所有库 show dbs
切换/创建数据库 use 数据库名称
查看所在库 db
删除库 db.dropDatabase()
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use student
switched to db student
> db
student
> db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

集合操作:

操作状态 操作指令
显示当前数据库集合 show collections
创建集合 db.createCollection(name)
删除集合 db.集合名称.drop()
> show collections
> db.createCollection('name')
{ "ok" : 1 }
> db.createCollection('age')
{ "ok" : 1 }
> show collections
age
name
> db.name.drop()
true
> db.age.drop()
true
>

三、文档操作:
添加文档数据:
db.集合名称.insert(document)
每一条数据就是一个文档,就是一个json。添加数据时候,如果不指定_id参数,MongoDB会自动为文档数据设置一个唯一的objectid

> db.stu.insert({'name':'zhangsan','age':18,'sex':'M','address':'beijing'})
WriteResult({ "nInserted" : 1 })

> db.stu.insert([
... {'name':'lisi','age':20,'sex':'F','address':'nanjing'},
... {'name':'wanger','age':21,'sex':'F','address':'beijing'},
... {'name':'mazi','age':19,'sex':'M','address':'shanghai'},
... ])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

查看文档数据:
db.集合名称.find([condition])
MongoDB 的条件查询语句非常强大,需要非常的细心,否则,容易出错。

操作状态 操作指令
查看集合中全部数据 db.集合名称.find()
格式化显示 db.集合名称.find().pretty()
条件查询 db.集合名称.find({'address':'beijing'})
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 20, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
> db.stu.find().pretty()
{
        "_id" : ObjectId("5cc13a668eb8751da29b855a"),
        "name" : "zhangsan",
        "age" : 18,
        "sex" : "M",
        "address" : "beijing"
}
{
        "_id" : ObjectId("5cc13ef48eb8751da29b855b"),
        "name" : "lisi",
        "age" : 20,
        "sex" : "F",
        "address" : "nanjing"
}
{
        "_id" : ObjectId("5cc13ef48eb8751da29b855c"),
        "name" : "wanger",
        "age" : 21,
        "sex" : "F",
        "address" : "beijing"
}
{
        "_id" : ObjectId("5cc13ef48eb8751da29b855d"),
        "name" : "mazi",
        "age" : 19,
        "sex" : "M",
        "address" : "shanghai"
}

> db.stu.find({'address':'beijing'})
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }

查询条件and,or,and or 混用:
在看and和or之前,我们先了解比较运算的符号:

操作符 描述
$ne 不等于
$gt 大于
$lt 小于
$gte 大于等于
$lte 小于等于
> db.stu.find({$and:[{'age':{$gte:18}},{'address':'beijing'}]})
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "name" : "zhangsan", "age" : 18, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
>

修改文档:
修改一条数据:db.集合名称.updata({条件},{修改值})

> db.stu.update({'name':'zhangsan'},{'age':30})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 20, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
>

可以看出,数据只剩下age:30
需要指定修改属性{$set:{age:20}}

> db.stu.update({'name':'lisi'},{$set:{'age':35}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 35, "sex" : "F", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "F", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }

更新集合中所有满足条件的文档{multi:true}

> db.stu.update({'sex':'F'},{$set:{'sex':'M'}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855b"), "name" : "lisi", "age" : 35, "sex" : "M", "address" : "nanjing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }

删除文档:

操作状态 操作指令
删除集合中所有的文档 db.table.remove({})
删除集合中满足条件的所有文档 db.table.remove({'sex':'F'})
只删除集合中满足条件的第一条文档: { justOne: true } db.table.remove({'sex':'F'}, { justOne: true })
> db.stu.remove({'name':'lisi'})
WriteResult({ "nRemoved" : 1 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855c"), "name" : "wanger", "age" : 21, "sex" : "M", "address" : "beijing" }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
> db.stu.remove({'sex':'M'},{justOne:true})
WriteResult({ "nRemoved" : 2 })
> db.stu.find()
{ "_id" : ObjectId("5cc13a668eb8751da29b855a"), "age" : 30 }
{ "_id" : ObjectId("5cc13ef48eb8751da29b855d"), "name" : "mazi", "age" : 19, "sex" : "M", "address" : "shanghai" }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容

  • 一、MongoDB简介 概述MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WEB应用提供...
    未央_m阅读 711评论 0 1
  • 一、MongoDB简介 概述MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WEB应用提供...
    EndEvent阅读 1,161评论 1 4
  • 一、MongoDB简介 1.概述 ​ MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WE...
    郑元吉阅读 976评论 0 2
  • 一、MongoDB简介 概述MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WEB应用提供...
    王梓懿_1fbc阅读 492评论 0 3
  • 一、MongoDB简介 概述MongoDB是一个基于分布式文件存储的数据库,由C++语言编写。旨在为WEB应用提供...
    fly5阅读 284评论 0 0