下面将介绍 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集合中的文档列表。