一.文档
文档是MongoDB的核心概念。文档就是键值对的一个有序集。注意:键是唯一的
如下:
{ "title":"test1", "content":"There is study", "date": new Date() }
二.集合
一组文档,相当与一张表,是动态模式
三.数据库
多个集合可以组成数据库
四.MongoDB shell简介
MongoDB 自带JavaScript shell,可在shell中使用命令行与MongoDB实例交互
shell中的基本操作
a.创建
insert
> post = {
... "title":"test1",
... "content":"There is study",
... "date": new Date()
... }
{
"title" : "test1",
"content" : "There is study",
"date" : ISODate("2020-07-28T05:49:53.951Z")
}
> db.blog.insert(post)
WriteResult({ "nInserted" : 1 })
b.读取
find
> db.blog.find()
{ "_id" : ObjectId("5f1f8fc39da18cf3e2cf8071"), "title" : "test1", "content" : "There is study", "date" : ISODate("2020-07-28T02:38:59.968Z"), "width" : 3 }
{ "_id" : ObjectId("5f1fb9909da18cf3e2cf8072"), "title" : "test2", "content" : "There is study", "date" : ISODate("2020-07-28T05:37:20.426Z") }
{ "_id" : ObjectId("5f1fba699da18cf3e2cf8073"), "title" : "test3", "content" : "There is study", "date" : ISODate("2020-07-28T05:40:57.366Z"), "study" : [ "english", "chinese" ] }
{ "_id" : ObjectId("5f1fbaf49da18cf3e2cf8074"), "title" : "test4", "content" : "There is study", "date" : ISODate("2020-07-28T05:43:16.673Z"), "study" : [ "english", "chinese" ], "lickly" : { "food" : "mifan", "ball" : "football" } }
{ "_id" : ObjectId("5f1fbca0837f599809534b75"), "title" : "test1", "content" : "There is study", "date" : ISODate("2020-07-28T05:49:53.951Z") }
findOne
> db.blog.findOne()
{
"_id" : ObjectId("5f1f8fc39da18cf3e2cf8071"),
"title" : "test1",
"content" : "There is study",
"date" : ISODate("2020-07-28T02:38:59.968Z"),
"width" : 3
}
c.更新
update,至少有两个参数:第一个是限定条件(用于匹配待更新的文档),第二个是新的文档
> post.conments = []
[ ]
> db.blog.update({"title":"test1"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find({"title":"test1"})
{ "_id" : ObjectId("5f1f8fc39da18cf3e2cf8071"), "title" : "test1", "content" : "There is study", "date" : ISODate("2020-07-28T05:49:53.951Z"), "conments" : [ ] }
d.删除
remove
> db.blog.remove({"title":"test1"})
五.数据类型
1.基本数据类型
.null
null用于表示空值或者不存在的字段
"conmments" : null
.布尔型
ture、false
.数值
"width" : 12.4
height:NumberInt(3)
.字符串
"title" : "test1"
.日期
"date" : new Date()
.正则表达式
{"title":/test/}
.数组
"study" : [ "english", "chinese" ]
.内嵌文档
"lickly" : { "food" : "mifan", "ball" : "football" }
.对象id
"_id" : ObjectId("5f1fb9909da18cf3e2cf8072")