带有“@"的 MongoDB 密码在里面 [英] MongoDB password with "@" in it

查看:51
本文介绍了带有“@"的 MongoDB 密码在里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Node.js 中的 Mongoose 使用用户名和密码连接到 MongoDB 数据库.所有的文档都说连接字符串应该看起来像

I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like

  mongodb://username:password@host:port/db

但是,密码中包含@"字符.我怎样才能从中创建一个猫鼬会理解的连接字符串?我可以转义密码中的@"还是必须使用其他连接方法?

However, the password contains the '@' character in it. How can I make a connection string out of this that mongoose will understand? Can I escape the '@' in the password or is there another method of connecting I have to use?

推荐答案

使用以下语法:

// use %40 for @
mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", { 
        useNewUrlParser: true
    }, function(err, db) {

    }
);

这篇关于带有“@"的 MongoDB 密码在里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆