使用db.auth()登录mongodb

本文将介绍怎样使用 db.auth("用户名", "密码") 方法更具用户名和密码登录到mongodb,然后访问数据库数据。

下面将介绍 db.auth() 的具体用法,假如你有用户名:test,密码:123456。下面将通过 mongo.exe 命令连接到本地 mongodb,如下:

D:mongodb-3.4.15in> mongo.exe                                       
MongoDB shell version v3.4.15-52-g874aa31cae      
connecting to: mongodb://127.0.0.1:27017          
MongoDB server version: 3.4.15-52-g874aa31cae     
> use test                                        
switched to db test                               
> db.auth("test", "123456")                       
1                                                 
> show collections                                
ex_entity_test                                    
ex_entity_test1                                   
test                                              
test2                                             
> db.test.find();
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc103"), "name" : "Bill", "type" : "A", "value" : 9 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc104"), "name" : "Perl", "type" : "A", "value" : 28 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc105"), "name" : "Tom", "type" : "A", "value" : 33 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc106"), "name" : "Helen", "type" : "B", "value" : 46 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc107"), "name" : "Luce", "type" : "B", "value" : 24 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc108"), "name" : "Ruby", "type" : "B", "value" : 34 }
{ "_id" : ObjectId("5e1dc7f1813be551fa5fc109"), "name" : "LuLi", "type" : "C", "value" : 21 }

其中:

  • use test:表示将使用test数据库,即use命令后将在 test 数据库中操作。

  • db.auth:对test数据库授权,即登录数据库,否则将不能正常操作。

  • show collections:显示test数据库中所有当集合列表。

  • db.test.find():查看test集合中的文档列表。

我们愈是学习,愈觉得自己的贫乏。 —— 雪莱
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号