温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB的常用命令有哪些

发布时间:2022-01-12 17:42:39 来源:亿速云 阅读:124 作者:柒染 栏目:开发技术

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();

看完上述内容,你们掌握MongoDB的常用命令有哪些的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI