文档(document)的数据结构和 JSON 基本一样。所有存储在集合中的数据都是 BSON 格式。
一、文档插入
db.collection.insert( <document or array of documents>, { writeConcern: <document>, ordered: <boolean> } )
例子:db.getCollection("user").insert({"name": "尔康"},{ordered:true})
- collection集合如果不存在,则会隐式创建
- mongo中的数字,默认情况下是double类型,如果要存整型,必须使用函数NumberInt(整型数字),否则取出来就有问题
- 插入当前日期使用new Data()
- 插入的数据没有指定_id,会自动生成主键值
- 如果某字段没值,可以赋值为null,或不写该字段
- MongoDB区分类型和大小写
参数 | 类型 | 作用 |
document | 文档或文档数组 | json格式的内容 |
writeConcren | 文档 | 可不设置。控制写入安全级别,描述MongoDB写入到mongod单实例,副本集,以及分片集群时何时应答给客户端。默认情况下,mongoDB文档增删改都会一直等待数据库响应(确认写入是否成功),然后才会继续执行 |
ordered | boolean | 可不设置,默认为true。若为真,按顺序插入文档,出现错误立即返回,不处理其他文档。若为假,则继续处理其他文档 |
1、Insert
(1)插入单个文档
db.CalendarTaskEntity.insert(
{
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "123",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829611179),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" : [
{
"responsiblePerson" : "15013223318",
"isFeedBack" : false
}
],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840143671)
});
(2)插入多个文档
db.CalendarTaskEntity.insert(
[{
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "113",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829611179),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840143671)
},
{"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "12233",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829704103),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840241041)
}]);
2、InsertOne
一次只能插入一条数据,如果插入多条的话,是会报错的
db.CalendarTaskEntity.insertOne(
{
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "123",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829611179),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840143671)
});
3、InsertMany
db.CalendarTaskEntity.insertMany(
[{
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "13",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829611179),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840143671)
},
{
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "12",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829704103),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840241041)
}]);
4、Save
save方法一次只能操作一条记录,操作多条的时候会报错
主键冲突时insertOne会报错,而save会直接用新值覆盖旧值
db.CalendarTaskEntity.save(
{
"_id": "6342741a4cb714e3c6337c55",
"orgId" : "d870d3e653b43da1a3d14ef30566386d",
"corpId" : "ww903454aaa2ef2240",
"calendarId" : "13",
"title" : "1",
"content" : "1",
"createPerson" : "wuweinian",
"initiator" : "wuweinian",
"createTime" : NumberLong(1663829611179),
"deadline" : NumberLong(1668755206565),
"status" : NumberInt(3),
"isDelete" : false,
"feedbackList" :
[ {
"responsiblePerson" : "15013223318",
"isFeedBack" : false
} ],
"_class" : "cn.com.do1.component.calendar.model.task.po.CalendarTaskPO",
"updatePerson" : "wuweinian",
"updateTime" : NumberLong(1663840143671)
});
二、文档删除
db.集合名称.remove(条件)
1、删除所有数据
db.CalendarTaskEntity.remove({})
2、删除指定条件
db.CalendarTaskEntity.remove({ "_id": "6342741a4cb714e3c6337c55"});