好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MongoDB的常用命令汇总(Mongo4.2.8)

一、数据库相关

1.切换/创建数据库

?

1

>use [dbname];

2.查询所有数据库

?

1

2

> show dbs;

mytest  0.000GB

3.查看当前使用的数据库

?

1

> db .getName();

Mytest

4.查看数据库版本

?

1

> db .version();

4.2.8

5.查看当前db的链接地址

?

1

> db .getMongo();

connection to 127.0.0.1:27017

二、用户相关

1、创建普通用户(创建用户cg,对mytest数据库读写权限)

?

1

> db .createUser({user: "cg" , pwd : "lianshi" ,roles:[{role: "readWrite" , db : "mytest" }]})

2、删除用户>db.dropUser("yonghu")

3、修改用户密码

?

1

db .updateUser( "cg" ,{ pwd : "123456" })

4、进入数据mytest,用户名密码认证

?

1

> db .auth( "cg" , "lianshi" );

三、集合Collection相关

1.获得数据聚合(表)

?

1

2

> db .getCollectionNames();

[ "student" ]

2. 集合(表)插入数据

?

1

db .student.insert({ "id" : "2" , "name" : "yxy" })

3.查询数据

?

1

2

3

4

5

> 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.查询唯一字段值

?

1

2

> db .student.distinct( "name" );

[ "hmf" , "yx" , "yxy" ]

会过滤掉name中的相同数据
相当于:select distict name from student;

5.查询name = yxy的记录

?

1

2

3

> 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字段类型的数据

?

1

db .student.insert({ "id" :NumberInt(1234567), "name" : "hu" });

7、插入int64字段类型数据

?

1

db .student.insert({ "age" :NumberLong(22), "name" : "hu" });

8、插入Decimal字段类型数据

?

1

db .student.insert({ "va" :NumberDecimal( "22.3" ), "name" : "hu" });

9、查询语句

?

1

2

3

4

db .student. find ({})

    .projection({})

    . sort ({_id:-1})

    .limit(100)

10、删除(集合)表

?

1

db .student.drop();

参考: http://HdhCmsTesttuohang.net/article/59989.html

到此这篇关于MongoDB的常用命令汇总(Mongo4.2.8)的文章就介绍到这了,更多相关MongoDB常用命令内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/fen_fen/article/details/106906344

查看更多关于MongoDB的常用命令汇总(Mongo4.2.8)的详细内容...

  阅读:21次