掌握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()
{
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18102
}
> db.userInfo.find({"age":23}).pretty()
{
"_id" : ObjectId("61b06f9bde6652f6f93ca02c"),
"name" : "Json",
"age" : 23,
"class" : 12101
}
>
2、指定返回键指定键的值为0,不返回该键值对;为1时返回。
> db.userInfo.find({},{"_id":0,"name":1,"class":1}).pretty()
{ "name" : "zhangsan", "class" : 19101 }
{ "name" : "lisi", "class" : 18102 }
{ "name" : "wangwu", "class" : 19124 }
{ "name" : "liliu", "class" : 19125 }
{ "name" : "Jhon", "class" : 19103 }
{ "name" : "Json", "class" : 12101 }
3、查询条件----比较操作符
第一篇文章详细写过了
$lt:小于(<),
$lte:小于等于(<=),
$gt:大于(>)
$gte:大于等于(>=),
$ne:不等于(≠)
4、查询条件----包含($in
)或不包含($nin
)
(1)需求:查询class在18102或12101的用户
> db.userInfo.find({"class":{$in:[18102,12101]}}).pretty()
{
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18102
}
{
"_id" : ObjectId("61b06f9bde6652f6f93ca02c"),
"name" : "Json",
"age" : 23,
"class" : 12101
}
(2)需求:查询class不在18102或12101的用户
> db.userInfo.find({"class":{$nin:[18102,12101]}}).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("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
}
>
5、查询条件----“$not”查询需求:查询名字中不存在“zhangsan”的用户信息。
> db.userInfo.find({"name":{$not:/zhangsan/}}).pretty()
{
"_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
}
>
6、特定类型查询----正则表达式需求:查询所有name为“zhangsan”的用户信息。
> 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"
]
}
>
删除文档(注意区分remove和delete)
注意:删除数据是永久性的,不能撤销,也不能恢复。所以删除之前先find()查看是否正确!
(1)db.集合名.deleteMany({})
需求:删除所有age大于22的用户信息。
> 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.deleteMany({age:{$gt:22}})
{ "acknowledged" : true, "deletedCount" : 3 }
> 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
}
>
(2)db.集合名.deleteOne({})
需求:删除age为15的用户信息。
> db.userInfo.deleteOne({age:15})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.userInfo.find().pretty()
{
"_id" : ObjectId("61b06cbfde6652f6f93ca028"),
"name" : "lisi",
"age" : 18,
"class" : 18102
}
{
"_id" : ObjectId("61b06e75de6652f6f93ca029"),
"name" : "wangwu",
"age" : 20,
"class" : 19124
}
>
(3)db.集合名.remove
1)需求:删除所有age为33的用户信息。
> 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.usInfo.remove({"age":33})
WriteResult({ "nRemoved" : 3 })
> db.usInfo.find().pretty()
{ "_id" : ObjectId("61b078a9de6652f6f93ca02f"), "age" : 23 }
{ "_id" : 1, "address" : "上海" }
{ "_id" : 2, "address" : "江苏" }
>
2)需求:删除age为23的用户信息。
> db.usInfo.remove({"age":23})
WriteResult({ "nRemoved" : 1 })
> db.usInfo.find().pretty()
{ "_id" : 1, "address" : "上海" }
{ "_id" : 2, "address" : "江苏" }
>