5.MongoDB 文档基本 CRUD

 

1.文档的插入

(1) 单个文档插入

语法:

1
> db.[collection_name].insert([json_data])
1
> db.[collection_name].save([json_data])

使用insert() 或 save() 方法向集合中插入文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
> db
test
> use articledb
switched to db articledb
> db
articledb
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
> show collections
1
2
3
4
5
6
7
8
9
> db.my_comment.insert({"articleid":"100000", "content": "today", "userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null})
WriteResult({ "nInserted" : 1 })
> show collections
my_comment
> show databases
admin 0.000GB
articledb 0.000GB
config 0.000GB
local 0.000GB

提示:

  1. comment集合如果不存在,则会隐式创建
  2. mongo中的数字,默认情况下是double类型,如果要存整型,必须使用函数NumberInt(整型数字),否则取出来就有问题了。
  3. 插入当前日期使用 new Date()
  4. 插入的数据没有指定 _id ,会自动生成主键值
  5. 如果某字段没值,可以赋值为null,或不写该字段。

注意:

  1. 文档中的键/值对是有序的。
  2. 文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
  3. MongoDB区分类型和大小写。
  4. MongoDB的文档不能有重复的键。
  5. 文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。

文档键命名规范:

  • 键不能含有\0 (空字符)。这个字符用来表示键的结尾。
  • .$ 有特别的意义,只有在特定环境下才能使用。
  • 以下划线”_”开头的键是保留的(不是严格要求的)。

(2) 批量插入

语法:

1
db.[collection_name].insertMany([{json_data}, {json_data}, ...])
1
2
> db.my_comment.insertMany([{"_id": "1","articleid": "100001","content": "我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。","userid": "1002","nickname": "相忘于江湖","createdatetime":new Date("2019-08-05T22: 08: 15.522Z"),"likenum":NumberInt(1000),"state": "1"},{"_id": "2","articleid": "100001","content": "我夏天空腹喝凉开水,冬天喝温开水","userid": "1005","nickname": "伊人憔悴","createdatetime":new Date("2019-08-05T23: 58: 51.485Z"),"likenum":NumberInt(888),"state": "1"},{"_id": "3","articleid": "100001","content": "我一直喝凉开水,冬天夏天都喝。","userid": "1004","nickname": "杰克船长","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state": "1"}])
{ "acknowledged" : true, "insertedIds" : [ "1", "2", "3" ] }

提示:

  • 插入时指定了 _id ,则主键就是该值。
  • 如果某条数据插入失败,将会终止插入,但已经插入成功的数据不会回滚掉。
  • 因为批量插入由于数据较多容易出现失败,因此,可以使用try catch进行异常捕捉处理,测试的时候可以不处理
    1
    2
    3
    4
    5
    try {
    db.[collection_name].insertMany([json_array]);
    } catch(e) {
    print(e);
    }

2.文档的基本查询

(1) 查询所有

语法:

1
> db.[collection_name].find()
1
2
3
4
5
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "articleid" : "100001", "content" : "我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。", "userid" : "1002", "nickname" : "相忘于江湖", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "3", "articleid" : "100001", "content" : "我一直喝凉开水,冬天夏天都喝。", "userid" : "1004", "nickname" : "杰克船长", "createdatetime" : ISODate("2019-08-06T01:05:06.321Z"), "likenum" : 666, "state" : "1" }

(2) 字段筛选条件

语法:

1
> db.[collection_name].find({})
1
2
3
4
> db.my_comment.find({"articleid" : "100001"})
{ "_id" : "1", "articleid" : "100001", "content" : "我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。", "userid" : "1002", "nickname" : "相忘于江湖", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 1000, "state" : "1" }
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 888, "state" : "1" }
{ "_id" : "3", "articleid" : "100001", "content" : "我一直喝凉开水,冬天夏天都喝。", "userid" : "1004", "nickname" : "杰克船长", "createdatetime" : ISODate("2019-08-06T01:05:06.321Z"), "likenum" : 666, "state" : "1" }
1
2
> db.my_comment.find({"articleid" : "100001", "userid" : "1005"})
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 888, "state" : "1" }

(3) 只返回符合条件的第一条数据

语法:

1
> db.[collection_name].findOne({})
1
2
3
4
5
6
7
8
9
10
11
> db.my_comment.findOne({"articleid" : "100001"})
{
"_id" : "1",
"articleid" : "100001",
"content" : "我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。",
"userid" : "1002",
"nickname" : "相忘于江湖",
"createdatetime" : ISODate("1970-01-01T00:00:00Z"),
"likenum" : 1000,
"state" : "1"
}

(4) 投影查询(Projection Query)

如果要查询结果返回部分字段,则需要使用投影查询(不显示所有字段,只显示指定的字段)

语法:

1
> db.[collection_name].find({[query_field]}, {[display_field]})

默认 _id 会显示

1
2
3
4
> db.my_comment.find({"articleid" : "100001"}, {articleid:1})
{ "_id" : "1", "articleid" : "100001" }
{ "_id" : "2", "articleid" : "100001" }
{ "_id" : "3", "articleid" : "100001" }
1
2
3
4
> db.my_comment.find({"articleid" : "100001"}, {articleid:1, _id:0})
{ "articleid" : "100001" }
{ "articleid" : "100001" }
{ "articleid" : "100001" }
1
2
3
4
> db.my_comment.find({"articleid" : "100001", "state": "1"}, {articleid:1, userid:1, state: 1, _id: 0})
{ "articleid" : "100001", "userid" : "1002", "state" : "1" }
{ "articleid" : "100001", "userid" : "1005", "state" : "1" }
{ "articleid" : "100001", "userid" : "1004", "state" : "1" }

3.文档的更新

(1) 覆盖修改

语法:

1
> db.[collection_name].update({[query_field]}, {[new_value]})
1
2
3
4
5
6
7
8
9
10
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "articleid" : "100001", "content" : "我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。", "userid" : "1002", "nickname" : "相忘于江湖", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 1000, "state" : "1" }
>
> db.my_comment.update({"_id": "1"}, {"likenum":NumberInt(1001)})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "likenum" : 1001 }

(2) 局部修改

语法:

1
> db.[collection_name].update({[query_field]}, {$set: {[new_value]}})
1
2
3
4
5
6
7
8
9
10
11
12
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "likenum" : 1001 }
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 888, "state" : "1" }
>
> db.my_comment.update({"_id": "2"}, {$set:{"likenum":NumberInt(999)}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "likenum" : 1001 }
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 999, "state" : "1" }

(3) 批量修改

语法:

1
> db.[collection_name].update({[query_field]}, {$set: {[new_value]}}, {multi: true})
1
2
3
> db.my_comment.find({"userid" : "1003"})
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2000, "state" : "1" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3000, "state" : "1" }

不添加 {multi: true} 只会修改第一个记录

1
2
3
4
5
> db.my_comment.update({"userid" : "1003"}, {$set: {"state" : "2"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_comment.find({"userid" : "1003"})
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2000, "state" : "2" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3000, "state" : "1" }

只有值不等的情况下才会更新,更新的记录数并不是查询匹配的记录数

1
2
3
4
5
> db.my_comment.update({"userid" : "1003"}, {$set: {"state" : "2"}}, {multi: true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })
> db.my_comment.find({"userid" : "1003"})
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2000, "state" : "2" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3000, "state" : "2" }
1
2
> db.my_comment.update({"userid" : "1003"}, {$set: {"state" : "1"}}, {multi: true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

(3) 列值增长修改

1
2
3
> db.my_comment.find({"userid" : "1003"})
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2000, "state" : "1" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3000, "state" : "1" }
1
2
> db.my_comment.update({"userid" : "1003"}, {$inc: {"likenum":NumberInt(1)}}, {multi: true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
1
2
3
> db.my_comment.find({"userid" : "1003"})
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2001, "state" : "1" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3001, "state" : "1" }

4.删除文档

语法:

1
> db.[collection_name].remove({[delete_condition]})
1
2
3
4
5
6
7
> db.my_comment.find()
{ "_id" : ObjectId("64199d422ad46f2e7a323d3f"), "articleid" : "100000", "content" : "today", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2023-03-21T12:04:18.184Z"), "likenum" : 10, "state" : null }
{ "_id" : "1", "likenum" : 1001 }
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 999, "state" : "1" }
{ "_id" : "3", "articleid" : "100001", "content" : "我一直喝凉开水,冬天夏天都喝。", "userid" : "1004", "nickname" : "杰克船长", "createdatetime" : ISODate("2019-08-06T01:05:06.321Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2001, "state" : "1" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3001, "state" : "1" }
1
2
> db.my_comment.remove({"state" : null})
WriteResult({ "nRemoved" : 2 })
1
2
3
4
5
> db.my_comment.find()
{ "_id" : "2", "articleid" : "100001", "content" : "我夏天空腹喝凉开水,冬天喝温开水", "userid" : "1005", "nickname" : "伊人憔悴", "createdatetime" : ISODate("1970-01-01T00:00:00Z"), "likenum" : 999, "state" : "1" }
{ "_id" : "3", "articleid" : "100001", "content" : "我一直喝凉开水,冬天夏天都喝。", "userid" : "1004", "nickname" : "杰克船长", "createdatetime" : ISODate("2019-08-06T01:05:06.321Z"), "likenum" : 666, "state" : "1" }
{ "_id" : "4", "articleid" : "100001", "content" : "专家说不能空腹吃饭,影响健康。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T08:18:35.288Z"), "likenum" : 2001, "state" : "1" }
{ "_id" : "5", "articleid" : "100001", "content" : "研究表明,刚烧开的水千万不能喝,因为烫嘴。", "userid" : "1003", "nickname" : "凯撒", "createdatetime" : ISODate("2019-08-06T11:01:02.521Z"), "likenum" : 3001, "state" : "1" }

删除全部文档:

语法:

1
> db.[collection_name].remove({})
1
2
> db.my_comment.remove({})
WriteResult({ "nRemoved" : 4 })
1
2
> db.my_comment.find()
>