非关系型数据库(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" }

你可能感兴趣的:(非关系型数据库(NoSql)--MongoDB)