MongoDB的常规操作


MongoDB的命令操作流程

先进入MongoDB数据库

首先查看所有数据库:

show dbs

1、创建demo数据库:
use demo

查看当前数据库:db
查看所有数据库:show dbs

因为demo还没有数据,所有无法显示出来

2、创建student集合:
db.createCollection("student")
db.createCollection("test")用于删除集合的时候查看数据有对比
查询集合:
show collections
show tables

demo数据库有一个以上集合或其他数据之后,就可以查找到了

3、插入数据:学号、姓名、性别、年龄、备注
db.student.insert({"Sno":15,"Sname":"陈志锋","Ssex":"男","Sage":21,"Sbz":"创造bug"})
db.student.insert({"Sno":16,"Sname":"钟靓仔","Ssex":"女","Sage":22,"Sbz":"寻找bug"})
db.student.insert({"Sno":17,"Sname":"赖靓仔","Ssex":"男","Sage":23,"Sbz":"解决bug"})
db.student.insert({"Sno":18,"Sname":"朱靓仔","Ssex":"女","Sage":24,"Sbz":"检查bug"})
db.student.insert({"Sno":19,"Sname":"黄靓仔","Ssex":"男","Sage":25,"Sbz":"审核项目"})

4、查询数据:
查看所有数据库:show dbs
查看集合所以数据:db.student.find()
根据学号索引来查询:db.student.find({"Sno":15})

5、修改数据:
db.student.update({"Sno":16},{$set:{"Ssex":"男"}})
db.student.find({"Sno":16})
db.student.update({"Sno":18},{$set:{"Ssex":"男"}})
db.student.find()

6、删除数据
①删除个别文档:
db.student.remove({"Sno":17})
db.student.find()
②删除整个集合:
db.student.drop()
show tables
③删除数据库:
db.createCollection("student")
db.dropDatabase()

 

 

视频讲解预览:https://www.bilibili.com/video/BV14K4y1b7Rm/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM