MongoDB的CRUD操作(入门)

MongoDB的简单介绍:

1:MongoDB是什么?

  • mongodb是非关系数据库 但是是非关系数据库当中功能最丰富,最像关系数据库的
  • MongoDB是一个基于分布式文件存储的数据库。
  • C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

2:MongoDB的三大特点:

  • 高性能
  • 易部署
  • 易使用
  • 存储数据非常方便
  • 支持 GolangRUBYPYTHONJAVAC++PHPC#等多种语言
  • 文件存储格式为BSON(一种JSON的扩展)。

3:先来介绍我们的插入语句吧

1:插入语句:

插入一条数据:

> db.jie.insert({name:"jie",age:18})
WriteResult({ "nInserted" : 1 })

可以使用:db.集合名.find()查看 

> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "jie", "age" : 18 }
这里id可以自己设置
可以指定id 不过最好不要指定

插入多条:

> db.jie.insert([{name:"luyunjie",age:18},{name:"jiebao",age:19}])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

mongodb也可以使用js语法 用for循环插入多条数据:

> for (var i=1;i<=10;i++){
db.jie1.insert({name: "jie"+i,age: i})
}
WriteResult({ "nInserted" : 1 })
这里不为10是因为for循环是一条一条插入
然后只显示最后一条插入的数量 单用find()查看还是可以查看10条数据

> db.jie1.find()
{ "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1", "age" : 1 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2", "age" : 2 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3", "age" : 3 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
{ "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10", "age" : 10 }

 

2:查询语句

基本的运算符介绍:

MongoDB的CRUD操作(入门)

 

 

 

出几个题目 配合查询语句来一一实现:

1:只查询字段name的值:

> db.jie1.find({},{name:1})
{ "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8" }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9" }
{ "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10" }
无论查询什么 id都会一直在的

2:查询age大于5 小于8的所有数据:

> db.jie1.find({age:{$gte:5,$lte:8}})
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
运用我们的运算符来达到效果 运算符的语法是
  db.集合名.find(

    键:{运算符:值}
)

3:查询age为1,4, 5, 9 的所有数据:

> db.jie1.find({age:{$in:[1,4,5,9]}})
{ "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1", "age" : 1 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
运用运算符达到效果

上面用我们的小案列来简单介绍一下mongodb的查询以后会慢慢更新的

 

3:修改(更新)语句:

修改器的基本介绍:

MongoDB的CRUD操作(入门)

 

 如果不用修改器来修改数据 则是替换

列子:

> db.jie.update({name:"jie"},{name:"xiaojie"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "luyunjie", "age" : 18 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
> 
到达的是替换的效果

我们继续举列子来讲解:

1:修改name为luyunjie的字段 把luyunjie改为xiaojiejie其他不变:

> db.jie.update({name:"luyunjie"},{$set:{name:"xiaojiejie"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 18 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
    修改成功 运用修改器
修改器语法:
db.集合名.updata(
    {修改的值},
    {修改器:{修改的值}
})

2:给age为20的数据递加或递减5:

递增:

> db.jie.update({age:20},{$inc:{age:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 25 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }

递减:

> db.jie.update({age:18},{$inc:{age:-5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 20 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
>

3:练习为修改器为修改多个值

题目:把name里面的数据改为小杰,age数据递增20,sex字段删除,dta字段改为bianhao;

准备数据:

> db.xiaojie.insert({name:"jie",age:20,sex:"boy",dta:"123"})
WriteResult({ "nInserted" : 1 })
> db.xiaojie.find()
{ "_id" : ObjectId("6069c59800fc7b062bb3eda1"), "name" : "jie", "age" : 20, "sex" : "boy", "dta" : "123" }

题目答案:

> db.xiaojie.update({name:"jie"},{$set:{name:"小杰"},$inc:{age:20},$rename:{dta:"bianhao"},$unset:{sex:true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xiaojie.find()
{ "_id" : ObjectId("6069c59800fc7b062bb3eda1"), "name" : "小杰", "age" : 40, "bianhao" : "123" }
这里有几个点 多条语句的写法如上 db.集合名.update
({修改字段:字段值}, {
修改器:{},
修改器:{},
修改器:{},
修改器:{}
})

 

4:删除语句:

删除语法是:

db.集合名.remove({条件},是否删除一条)

1:删除一条数据:

> db.jie1.remove({},true)
WriteResult({ "nRemoved" : 1 })
> db.jie1.find()
{ "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2", "age" : 2 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3", "age" : 3 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
{ "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10", "age" : 10 }

2:删除全部数据:

> db.jie1.remove({})
WriteResult({ "nRemoved" : 9 })

简单介绍了一下MongoDB的CRUD操作 这篇博客会慢慢完善成MongoDB笔记 加油一起努力!

 

上一篇:运维必经操作,TcaplusDB tcapsvr扩缩容介绍


下一篇:Java异常与调优一站式解决方案