总结MongoDB数据库的基础操作
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了总结MongoDB数据库的基础操作,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。本文实例讲述了MongoDB数据库基础操作。分享给大家供大家参考,具体如
千家信息网最后更新 2025年11月07日总结MongoDB数据库的基础操作
这篇文章主要讲解了总结MongoDB数据库的基础操作,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
本文实例讲述了MongoDB数据库基础操作。分享给大家供大家参考,具体如下:
1.创建数据库
>use test > db.test.insert({"name":1})- 插入之后才能查到test
2.查看数据库
>show dbs
3.删除数据库
> use test > db.dropDatabase()
4.创建集合
4.1 集合概念
- 集合就是一组文档,相当于多条记录。
> db.title.insert({"name":"hyx"})- 插入之后即创建集合
5.查看集合
> show collections
6.删除集合
>use test >db.title.drop()
7.插入文档
7.1 文档概念
- 多个键及其关联的值有序地放置在一起就是文档。
- 文档类似于json数据
> db.file.insert({name:"huangyuxin",age:11})8.查看文档
>db.files.find()
9.变量方式插入文档
> document=({by:"hyx"}){ "by" : "hyx" }> db.file.insert(document)WriteResult({ "nInserted" : 1 })> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }>10.同时插入多条
> var res = db.file.insertMany([{"b": 3}, {'c': 4}])> res{ "acknowledged" : true, "insertedIds" : [ ObjectId("5c6e8bba0fc535200b893f2b"), ObjectId("5c6e8bba0fc535200b893f2c") ]}> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }>11.更新文档
> db.file.update({"name":"huangyuxin"},{$set:{"name":"hyx"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }>> db.file.save({"_id" : ObjectId("5c6e8b1c0fc535200b893f2a"),"name":"hyx"})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }>12.删除文档
12.1删除指定文档
> db.title.find(){ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }> db.file.remove({"b":3})WriteResult({ "nRemoved" : 1 })> db.file.find(){ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }>12.2删除全部文档
>db.file.deleteMany({})12.3删除多个文档
>db.file.deleteMany({ status : 1 })- 删除当前库所有status 等于 1 的文档
13.条件表达式
13.1$gt 大于
- 查询age 大于 0 的数据
> db.title.find({age:{$gt : 0}}){ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }>13.2 $lt 小于
13.3 $gte 大于等于 $lte 小于等于
- 查询age 大于等于 0 的数据
> db.title.find({age:{$gte : 1}})13.4 大于小于
> db.title.find({age:{$lt:13,$gt:10}}){ "_id" : ObjectId("5c6f7ded3ea8783bbfb7fd5f"), "age" : 12 }{ "_id" : ObjectId("5c6f7e833ea8783bbfb7fd60"), "age" : 12 }>13.5 $ne 不等于 $eq 等于
14. $type操作符
- $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。
> db.title.find({"name" : {$type : 2}}){ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }>15. limit()
- 查询指定条数
> db.title.find().limit(2){ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }{ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }>- 第一个 {} 放 where 条件,为空表示返回集合中所有文档。
- 第二个 {} 指定那些列显示和不显示 (0表示不显示 1表示显示)。
> db.title.find({},{"name":1,_id:0}).limit(1){ "name" : "yx" }>16.skip()
- 跳过几条数据
- 不要轻易使用Skip来做查询,否则数据量大了就会导致性能急剧下降,这是因为skip是一条一条的数过来的,多了自然就慢了。
17.sort()
- 1 为升序排列,而 -1 是用于降序排列。
> db.title.find({},{'age':1,_id:0}).sort({age:1}){ }{ "age" : 10 }{ "age" : 12 }{ "age" : 12 }> db.title.find({},{'age':1,_id:0}).sort({age:-1}){ "age" : 12 }{ "age" : 12 }{ "age" : 10 }{ }>18.索引
18.1 创建单个索引
- 1 为指定按升序创建索引,降序索引指定为 -1
>db.title.createIndex({"age":1})18.2 创建多个索引
>db.title.createIndex({"name":1,"age":-1})18.3 查看索引
>db.col.getIndexes()
18.4 查看索引大小
>db.col.totalIndexSize()
18.5 删除所有集合索引
>db.col.dropIndexes()
18.6 删除指定索引
>> db.title.dropIndex({'age':1}){ "nIndexesWas" : 2, "ok" : 1 }>看完上述内容,是不是对总结MongoDB数据库的基础操作有进一步的了解,如果还想学习更多内容,欢迎关注行业资讯频道。
文档
数据
索引
数据库
查询
基础
内容
多个
升序
多条
就是
操作符
条件
概念
类型
学习
有序
兴趣
单个
变量
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发公司工具
网络技术研究所招聘
区块链的六大网络安全应用
免费 云数据库 mysql
戴尔服务器开机后黑屏
武汉元启星互联网科技
一刀传世服务器冠名权是干什么
我的世界服务器锁定别人基地坐标
办公网络安全策略
英雄联盟显示服务器丢失
服务器批处理文件定时运行
数字货币的网络安全吗
非你莫属网络安全选手
软件开发人员用博客吗
怎么启动帐套管理服务器
方舟生存进化964服务器
浙江品牌软件开发电话多少
平台直播软件开发
数据库触发
美洲新世界服务器容量
带检索功能的数据服务器
edna实时数据库中文版
oracle数据库表设计
中国列车网络技术的发展情况
水文数据库
目前最火的软件开发行业
psu服务器保护密钥
酒店软件开发费用是多少
u盘怎么进入网络安全模式
exe软件开发入门