您现在的位置是:网站首页> 编程资料编程资料
MongoDB的常用命令汇总(Mongo4.2.8)_MongoDB_
2023-05-27
507人已围观
简介 MongoDB的常用命令汇总(Mongo4.2.8)_MongoDB_
一、数据库相关
1.切换/创建数据库
>use “dbname”;
2.查询所有数据库
> show dbs; mytest 0.000GB
3.查看当前使用的数据库
> db.getName();
Mytest
4.查看数据库版本
> db.version();
4.2.8
5.查看当前db的链接地址
> db.getMongo();
connection to 127.0.0.1:27017
二、用户相关
1、创建普通用户(创建用户cg,对mytest数据库读写权限)
> db.createUser({user:"cg",pwd:"lianshi",roles:[{role:"readWrite",db:"mytest"}]})2、删除用户>db.dropUser("yonghu")
3、修改用户密码
db.updateUser("cg",{pwd:"123456"})4、进入数据mytest,用户名密码认证
> db.auth("cg","lianshi");三、集合Collection相关
1.获得数据聚合(表)
> db.getCollectionNames(); [ "student" ]
2. 集合(表)插入数据
db.student.insert({"id":"2","name":"yxy"})3.查询数据
> db.student.find(); { "_id" : ObjectId("5eef61f3447efbc4346fbb9b"), "id" : "2", "name" : "yxy" } { "_id" : ObjectId("5eef61fe447efbc4346fbb9c"), "id" : "1", "name" : "hmf" } { "_id" : ObjectId("5eeff9582e8cdcf5c32c0ecf"), "id" : "3", "name" : "yx" } 相当于:select* from student;4.查询唯一字段值
> db.student.distinct("name"); [ "hmf", "yx", "yxy" ]会过滤掉name中的相同数据
相当于:select distict name from student;
5.查询name = yxy的记录
> db.student.find({"name":"yxy"}); { "_id" : ObjectId("5eef61f3447efbc4346fbb9b"), "id" : "2", "name" : "yxy" } { "_id" : ObjectId("5ef077145c4ca32ccc787893"), "id" : "2", "name" : "yxy" }相当于: select * from student where name = “yxy”;
6.插入int32字段类型的数据
db.student.insert({"id":NumberInt(1234567),"name":"hu"});7、插入int64字段类型数据
db.student.insert({"age":NumberLong(22),"name":"hu"});8、插入Decimal字段类型数据
db.student.insert({"va":NumberDecimal("22.3"),"name":"hu"});9、查询语句
db.student.find({}) .projection({}) .sort({_id:-1}) .limit(100)10、删除(集合)表
db.student.drop();
参考:https://www.jb51.net/article/48217.htm
到此这篇关于MongoDB的常用命令汇总(Mongo4.2.8)的文章就介绍到这了,更多相关MongoDB常用命令内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- mongodb三分钟入门大全_MongoDB_
- MongoDB4.28开启权限认证配置用户密码登录功能_MongoDB_
- MongoDB的常用命令汇总(Mongo4.2.8)_MongoDB_
- mongodb启动方法小结_MongoDB_
- MongoDB4.28开启权限认证配置用户密码登录功能_MongoDB_
- mongodb启动方法小结_MongoDB_
- 关于CentOS 8 搭建MongoDB4.4分片集群的问题_MongoDB_
- 批量备份还原导入与导出MongoDB数据方式_MongoDB_
- 在mac系统下安装与配置mongoDB数据库_MongoDB_
- 关于对MongoDB索引的一些简单理解_MongoDB_
