MongoDB对数据的CRUD(12458字总结)
Posted xingweikun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB对数据的CRUD(12458字总结)相关的知识,希望对你有一定的参考价值。
掌握MongoDBCRUD基本操作
插入文档–批量插入
MongoDB使用insert()方法向集合中插入文档,语法如下:
db.集合名.insert(document)
注意:_id是文档的唯一标识
查看已插入文档:db.集合名.find()
或db.集合名.find().pretty()
(1)批量插入----insert([…,…,……])
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
xwk 0.000GB
>
> db.userInfo.insert(["name":"zhangsan","age":16,"class":19101,"name":"lisi","age":18,"class":18101])
BulkWriteResult(
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
)
> db.userInfo.find().pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 16,
"class" : 19101
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18101
(2)批量插入----insert(x)
> x=["name":"wangwu","age":20,"class":19124,"name":"liliu","age":32,"class":19125]
[
"name" : "wangwu",
"age" : 20,
"class" : 19124
,
"name" : "liliu",
"age" : 32,
"class" : 19125
]
>
> db.userInfo.insert(x)
BulkWriteResult(
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
)
> db.userInfo.find().pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 16,
"class" : 19101
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18101
"_id" : ObjectId("61b06e75de6652f6f93ca029"),
"name" : "wangwu",
"age" : 20,
"class" : 19124
"_id" : ObjectId("61b06e75de6652f6f93ca02a"),
"name" : "liliu",
"age" : 32,
"class" : 19125
>
(3)批量插入----insertMany([…,…,……])
> db.userInfo.insertMany(["name":"Jhon","age":41,"class":19103,"name":"Json","age":23,"class":12101])
"acknowledged" : true,
"insertedIds" : [
ObjectId("61b06f9bde6652f6f93ca02b"),
ObjectId("61b06f9bde6652f6f93ca02c")
]
> db.userInfo.find().pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 16,
"class" : 19101
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18101
"_id" : ObjectId("61b06e75de6652f6f93ca029"),
"name" : "wangwu",
"age" : 20,
"class" : 19124
"_id" : ObjectId("61b06e75de6652f6f93ca02a"),
"name" : "liliu",
"age" : 32,
"class" : 19125
"_id" : ObjectId("61b06f9bde6652f6f93ca02b"),
"name" : "Jhon",
"age" : 41,
"class" : 19103
"_id" : ObjectId("61b06f9bde6652f6f93ca02c"),
"name" : "Json",
"age" : 23,
"class" : 12101
>
更新文档
一:update()
update()方法用于更新已存在的文档。语法格式如下:
db.集合名.update(
<query>,
<update>,
upsert:<boolean>,
multi:<boolean>,
writeConcern:<document>
)
参数说明:
query:update的查询条件,类似sqlupdate查询内where后面的。
update:update的对象和一些更新的操作符(如$,$inc...
)等,也可以理解为sql update查询内set后面的
upsert:可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi:可选,mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern:可选,抛出异常的级别。
(1)更新文档——修改器$inc(增加和减少,只能针对数字类型)修改name为“zhangsan”的age增加3
> db.userInfo.update("name":"zhangsan",$inc:"age":3)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 19,
"class" : 19101
>
修改name为“zhangsan”的age减少4
> db.userInfo.update("name":"zhangsan",$inc:"age":-4)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101
>
(2)更新文档——修改器$set(可以完成特定需求的修改)修改name为“lisi”的class为18102
> db.userInfo.find(name:"lisi").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18101
> db.userInfo.update("name":"lisi",$set:"class":18102)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"lisi").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18102
>
(3)更新文档——修改器$push(可以完成数组的插入)修改name为“zhangsan”的文档中插入数组“comments”
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101
> db.userInfo.update("name":"zhangsan",$push:"comments":"name":"leon","email":"123@qq.com")
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101,
"comments" : [
"name" : "leon",
"email" : "123@qq.com"
]
(4)更新文档——修改器$addToSet(向数组中添加一个信息)向email数组中添加一个email信息:
> db.userInfo.update(name:"zhangsan",$addToSet:"email":"456@qq.com")
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101,
"comments" : [
"name" : "leon",
"email" : "123@qq.com"
],
"email" : [
"456@qq.com"
]
> db.userInfo.update(name:"zhangsan",$addToSet:"email":"789@qq.com")
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.userInfo.find(name:"zhangsan").pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101,
"comments" : [
"name" : "leon",
"email" : "123@qq.com"
],
"email" : [
"456@qq.com",
"789@qq.com"
]
>
(5)更新文档——更新多个文档把所有“age”为22的数据改为33
> db.usInfo.insert("age":22,"age":23,"age":22)
WriteResult( "nInserted" : 1 )
> db.usInfo.find().pretty()
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 22
> db.usInfo.insert(["age":22,"age":23,"age":22])
BulkWriteResult(
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
)
> db.usInfo.find().pretty()
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 22
"_id" : ObjectId("61b078a9de6652f6f93ca02e"), "age" : 22
"_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23
"_id" : ObjectId("61b078a9de6652f6f93ca030"), "age" : 22
>
> db.usInfo.update("age":22,$set:"age":33,false,true)
WriteResult( "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 )
> db.usInfo.find().pretty()
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02e"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23
"_id" : ObjectId("61b078a9de6652f6f93ca030"), "age" : 33
>
二:save()
db.collection.save(object)
参数说明:
object代表需要更新的对象,如果集合内部已经存在一个和object相同的"_id"的记录,MongoDB会把object对象替换集合内已存在的记录,如果不存在,则会插入object对象。
> db.usInfo.insert("_id":001,"address":"北京")
WriteResult( "nInserted" : 1 )
> db.usInfo.find().pretty() )
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02e"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23
"_id" : ObjectId("61b078a9de6652f6f93ca030"), "age" : 33
"_id" : 1, "address" : "北京"
> db.usInfo.save("_id":001,"address":"上海")))
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )
> db.usInfo.find().pretty() )
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02e"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23
"_id" : ObjectId("61b078a9de6652f6f93ca030"), "age" : 33
"_id" : 1, "address" : "上海"
>
> db.usInfo.save("_id":002,"address":"江苏")
WriteResult( "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 )
> db.usInfo.find().pretty() )
"_id" : ObjectId("61b0787dde6652f6f93ca02d"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02e"), "age" : 33
"_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23
"_id" : ObjectId("61b078a9de6652f6f93ca030"), "age" : 33
"_id" : 1, "address" : "上海"
"_id" : 2, "address" : "江苏"
>
查询文档
语法:db.集合名.find(query,projection)
参数说明:
query:可选,使用查询操作符指定查询条件。
projection:可选,指定返回的键。
1、查询所有数据:空的查询文档会匹配集合的全部内容。若是不指定查询文档,默认就是
> db.userInfo.find().pretty()
"_id" : ObjectId("61b06cbfde6652f6f93ca027"),
"name" : "zhangsan",
"age" : 15,
"class" : 19101,
"comments" : [
"name" : "leon",
"email" : "123@qq.com"
],
"email" : [
"456@qq.com",
"789@qq.com"
]
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18102
"_id" : ObjectId("61b06e75de6652f6f93ca029"),
"name" : "wangwu",
"age" : 20,
"class" : 19124
"_id" : ObjectId("61b06e75de6652f6f93ca02a"),
"name" : "liliu",
"age" : 32,
"class" : 19125
"_id" : ObjectId("61b06f9bde6652f6f93ca02b"),
"name" : "Jhon",
"age" : 41,
"class" : 19103
"_id" : ObjectId("61b06f9bde6652f6f93ca02c"),
"name" : "Json",
"age" : 23,
"class" : 12101
>
> db.userInfo.find("name":"lisi").pretty()
以上是关于MongoDB对数据的CRUD(12458字总结)的主要内容,如果未能解决你的问题,请参考以下文章
python3操作MongoDB的crud以及聚合案例,代码可直接运行(python经典编程案例)
Spring MVC,Maven,CRUD,MongoDB [关闭]