MongoDB Databases, Collections,Documents
MongoDB基础知识
MongoDB是一种数据库系统,属于一类所谓的NoSQL数据库系统,它基于不同于关系模型的数据模型以及不同于SQL的数据定义、操作、检索和管理语言。
MongoDB数据模型(BSON)是基于key:value对的概念,将key:value对分组成文档和数组。
MongoDB数据库系统运行在许多数据库上。
MongoDB数据库是一组集合
MongoDB集合是一组文档
MongoDB文档是一组键:值对。
MongoDB值可以是原子值,也可以是文档或数组。
MongoDB原子值是BSON规范中包含的类型之一,如数字、字符串、日期等。
MongoDB数组是一个值序列。
每个MongoDB key:value对在一个文档中必须有一个唯一的key。
每个MongoDB文档必须集合中有一个唯一的标识符。
每个MongoDB集合在数据库中必须有一个唯一的名称。
让我们来举一个例子:
{"_id": ObjectId(),
"full name": {"first name":"James",
"initials":null,
"last name":"Bond"},
"employee number":"007",
"skills":["cooking", "painting", "gardening"],
"cars owned":[ {"rego":"233",
"made":"Porsche"},
{"rego":"566",
"made":"Ferrari"} ],
"secret codes":[ [1,2,3,4],
[9,8,7,5] ],
"date of birth":new Date("2021-01-12")
}
服务器
使用选项–dbpath, --port和 --bind_ip启动MongoDB服务器:
mongod --dbpath data --port 4000 --bind_ip 192.168.2.100
使用选项——dbpath、——port和运行在本地主机上的服务器启动MongoDB服务器:
mongod --dbpath data --port 4000 --bind_ip localhost
/*简化一下 */
mongod --dbpath data --port 4000
基于shell启动MongoDB命令。
mongo --port 4000
/*查看帮助 */
db.help()
数据库
设置当前数据库
use database-name
例如使用数据库JFM
use JFM
创建并切换到一个新的数据库JFH
use JFH
展示数据库的清单
show dbs
运行结果如下:
JFM 0.000GB
JFH 0.000GB
集合 (Collections)
创建带有空文档的新集合:
db.JFM.insert({})
列出集合的内容:
db.mycol.find()
{ "_id" : ObjectId("57e385f8ffc660a351b58010") }
查看集合清单:
show collections
JFM
文档(Documents)
创建一个新的非空文档:
db.JFM.insert({"one":"1", "many ones":[1,1,1,1]})
列出集合的内容:
db.JFM.find()
{ "_id" : ObjectId("57e385f8ffc660a351b58010") }
{ "_id" : ObjectId("57e38cbeffc660a351b58012"),
"one" : "1", "many ones" : [ 1, 1, 1, 1 ] }
格式化(Formatting)
列出集合中格式化良好的内容:
/*多用于展示 */
{ "_id" : ObjectId("57e385f8ffc660a351b58010") }
{ "_id" : ObjectId("57e38cbeffc660a351b58012"),
"one" : "1",
"many ones" : [ 1,
1,
1,
1
]
}
DDL
从集合中删除所有文档:
db.JFM.remove({})
删除集合:
db.JFM.drop()
删除数据库:
db.dropDatabase()
DML
我们创建一个js文件包含以下内容:
b.mycol.insert({"CITY": {"name":"JiNan",
"population":"890W",
"country":"China",
"state":"ShanDong"} });
db.mycol.insert({"EMPLOYEE":{"enum":1234567,
"full-name":"JFM",
"salary": "1K",
"hobbies":["Studying",
"Sleeping",
"Eating"]} })
处理脚本时将两个文档插入集合JFM中:
load("data.js")
列出集合JFM的内容:
db.JFM.find().pretty()
输出结果如下:
{
"_id" : ObjectId("57e3c817fe6a1bfd5105022a"),
"CITY" : {
"name" : "JiNan",
"population" : "890W",
"country" : "China",
"state" : "New ShanDong Wales"
}
}
{
"_id" : ObjectId("57e3c817fe6a1bfd5105022b"),
"EMPLOYEE" : {
"enum" : 1234567,
"full-name" : "JFMa",
"salary" : "1K",
"hobbies":["Studying",
"Sleeping",
"Eating"
]
}
}
查询语句
find()用于将模式与集合中的文档匹配:
db.department.find({"age":25})
/*如果括号里为空则返回所有集合*/
db.department.find({})
查找集合中文档的总数:
db.department.count()
先查询再数被查询内容的数量:
db.department.find({"code":"SOPH"}).count()
db.department.count({"code":"SOPH"})
按照两个条件查询:
db.department.find({"name":"School of Physics", "code":"SOPH"}
比较"key"=“value”
{"key":"value"}
{"key": {$eq:"value"}}
比较“key" > “value”
{"key": {$gt:"value"}}
gt:great than,方便记忆。
同理 lt。
(“key1”=“value1”) or (“key2”=“value2”):
$or: [{"key1":"value1"},{"key2":"value2"}]}
(“key1”=“value1”) and (“key2”=“value2”)
{$and: [{"key1":"value1"},{"key2":"value2"}]}
((“key1”=“value1”) or (“key2”=“value2”)) and (“key3”=“value3”)
{$and: [{$or: [{"key1":"value1"},{"key2":"value2"}]},{"key3"="value3"}]}
“key” not =“value”
{"key": {$not: {$eq:"value"}}}
not ((“key1”=“value1”) or (“key2”=“value2”))
{$nor: [{"key1":"value1"},{"key2":"value2"}]}
References
- Chodorow K. MongoDB The De?nitive Guide, O’Reilly, 2013, Chapter 2.
- MongoDB Architecture, https://www.mongodb.com/mongodb-architecture