> db.users.insert({"name" : "xiaomotong", "age" : 18})
> db.users.insert({name:"xiaozhu",age:15,hobby:"basketball",infos:{tall:190,height:70},school:"sh"})
> db.users.insertMany([{name:"xiaopang"},{name:"wangwu"}])
> db.users.insertMany([{name:"nancy", "age" :25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" },{name:"job", "age" : 19, "hobby" : "basketball", "infos" : { "tall" : 170, "height" : 70 }, "school" : "nj" }])
mongodb 在插入数据上面还有这么多函数方法可以使用,各取所需
> db.users.find()
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaomotong", "age" : 18 }
...
> db.users.update({name:"xiaomotong"},{$set:{name:"xiaokeai",age:25,hobby:"reading",infos:{tall:175,height:62},school:"cs"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
使用 $set 表示需要更新字段
查询数据的时候我们发现,mongodb 自动在我们的文档中加入了 _id 字段,这是一个主键,如果不自己设置的话,mongodb 为默认给我们加上,是一个 24 位的 uuid
关于更新的方法,官网还提供了这些方法:
> db.users.find({"infos.tall":{$gt:170}})
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }
此处的 $gt 是大于 的意思,上期有说到这些运算符,再来回顾一遍
上述 infos.tall
属于子查询,表示 infos 是一个内嵌文档,需要查找的是 文档中的 tall 字段,我们也不能不加双引号,因为系统会默认识别 infos.tall 为一个字段,实际上是找不到这个字段的,因此是查询不到结果的,如下报错:
> db.users.find({infos.tall:{$gt:170}})
2021-10-04T09:39:21.349+0800 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):1:20
以更加友好的方式查询数据展示出来
> db.users.find({"infos.tall":{$gte:180}}).pretty()
{
"_id" : ObjectId("615a56d6bc6afecd2cff8f96"),
"name" : "xiaozhu",
"age" : 15,
"hobby" : "basketball",
"infos" : {
"tall" : 190,
"height" : 70
},
"school" : "sh"
}
此处的 db.users.find({"infos.tall":{$gte:180}})
就类似于关系型数据库的 sql 语句:select * from users where "infos.tall" >= 180
例如查询,身高在 是 175 或者 190 的学生.
此处的 db.users.find({"infos.tall":{$in:[190,175]}})
就类似与关系型数据库的 select * from users where "infos.tall" in (190,175)
演示一下简单的正则表达式:
> db.users.find({name:/^w/}).pretty()
{ "_id" : ObjectId("615a5856d988690b07c69f65"), "name" : "wangwu" }
> db.users.find({name:/g$/}).pretty()
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
此处在我们查询的时候,/正则表达式/,mongodb 会自动识别并按照正则表达式的规则进行匹配和处理
我们也可以查询 null
> db.users.find({infos:null})
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5856d988690b07c69f65"), "name" : "wangwu" }
> db.users.find({infos:{$exists:false}})
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
查询字段为 null 可以使用第一种方式,查询字段不存在可以使用第二种方式
> db.users.deleteOne({name:"wangwu"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.users.find({name:/^w/}).pretty()
>
常用的删除方法有:
> use mytest
switched to db mytest
> db.users.find()
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }
{ "_id" : ObjectId("615a5917d988690b07c69f67"), "name" : "job", "age" : 19, "hobby" : "basketball", "infos" : { "tall" : 170, "height" : 70 }, "school" : "nj" }
> db.users.find().limit(2)
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }
> db.users.find().skip(2).limit(2)
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }
db.users.find()
查询所有的数据,按照默认的顺序打印出来db.users.find().limit(2)
从前面的结果中,限制取出 2 条db.users.find().skip(2).limit(2)
从前面的结果中,偏移 2 条,再限制取出 2 条下一次,梳理聚合相关内容
官方一手资料:https://docs.mongodb.com/manual/reference
朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力
好了,本次就到这里
技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。
我是阿兵云原生,欢迎点赞关注收藏,下次见~