要登录到MongoDB的指定数据库,可以使用以下方法:
mongo --host <host> --port <port> -u <username> -p <password> --authenticationDatabase <authDatabase> <database>
其中,<host>和<port>是MongoDB服务器的主机名和端口号,<username>和<password>是登录的用户名和密码,<authDatabase>是用于身份验证的数据库,<database>是要登录的目标数据库。
const mongoose = require('mongoose');
mongoose.connect('mongodb://<username>:<password>@<host>:<port>/<database>', { useNewUrlParser: true, useUnifiedTopology: true });
其中,<username>、<password>、<host>、<port>和<database>分别是登录所需的用户名、密码、主机名、端口号和目标数据库名。
from pymongo import MongoClient
client = MongoClient('<host>', <port>)
db = client[<authDatabase>]
db.authenticate('<username>', '<password>')
target_db = client[<database>]
其中,<host>、<port>、<authDatabase>、<username>、<password>和<database>分别是MongoDB服务器的主机名、端口号、用于身份验证的数据库名、登录用户名、密码和目标数据库名。
请注意,上述示例中的<host>和<port>是MongoDB服务器的实际主机名和端口号,<username>和<password>是有效的用户名和密码,<authDatabase>是有效的用于身份验证的数据库名,<database>是有效的目标数据库名。你需要根据自己的MongoDB服务器和数据库配置进行相应的替换。