这篇文章介绍了MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
一、 数据库使用
开启 mongodb 服务:要管理数据库,必须先开启服务,开启服务使用
mongod --dbpath c:\mongodb
管理 mongodb 数据库:(一定要在新的 cmd 中输入)
mongo
清屏:
cls
查看所有数据库列表
show dbs
二、 创建数据库
使用数据库、创建数据库
use student
如果真的想把这个数据库创建成功,那么必须插入一个数据。
数据库中不能直接插入数据,只能往集合(collections)中插入数据。不需要专门创建集合,只
需要写点语法插入数据就会创建集合:
插入一条数据
db.student.insert({“name”:”xiaoming”});
db.student 系统发现 student 是一个陌生的集合名字,所以就自动创建了集合。
显示当前的数据集合(mysql 中叫表)
show collections
删除数据库,删除当前所在的数据库
db.dropDatabase();
删除集合,删除指定的集合 删除表
删除集合
db.COLLECTION_NAME.drop()
db.user.drop()
三、插入(增加)数据
插入数据,随着数据的插入,数据库创建成功了,集合也创建成功了。
db. 表名.insert({"name":"zhangsan"}); student 集合名称(表)
四、查找数据
1 、查询所有记录
db.userInfo.find();
相当于:select* from userInfo;
2 、查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct("name");
会过滤掉 name 中的相同数据
相当于:select distict name from userInfo;
3 、查询 age = 22 的记录
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;
4 、查询 age > 22 的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
5 、查询 age < 22 的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;
6 、查询 age >= 25 的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
7 、查询 age <= 25 的记录
db.userInfo.find({age: {$lte: 25}});
8 、查询 age >= 23 并且 age <= 26 注意书写格式
db.userInfo.find({age: {$gte: 23, $lte: 26}});
9 、查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.userInfo.find({name: /mongo
本文标题为:MongoDB实现创建删除数据库、创建删除表(集合 )、数据增删改查


基础教程推荐
- redis 数据库 2023-09-13
- oracle数据库排序后如何获取第一条数据 2023-07-24
- redis乐观锁与悲观锁的实战 2023-07-13
- Python安装第三方库的方法(pip/conda、easy_install、setup.py) 2023-07-28
- Java程序员从笨鸟到菜鸟(五十三) 分布式之 Redis 2023-09-11
- Mariadb数据库主从复制同步配置过程实例 2023-07-25
- SQL Server如何设置用户只能访问特定数据库和访问特定表或视图 2023-07-29
- oracle19c卸载教程的超详细教程 2023-07-23
- Windows10系统中Oracle完全卸载正确步骤 2023-07-24
- Python常见库matplotlib学习笔记之画图中各个模块的含义及修改方法 2023-07-27