node.js - 如何使用以美元符号开头的字符串值更新 mongodb 中的字段
问题描述
我正在尝试将散列密码保存到 mongodb,但问题似乎是 bcrypt 给出的结果字符串以美元符号开头。不是更新该字段,而是删除该字段。
以下是相关代码:
const hash = await bcrypt.hash(password, 10);
collection.updateOne({ email }, [
{ $set: { hash } }
]);
解决方案
那么应该是这个
{ $set: { hash: hash } }
或者让它更清楚一点:
const hash_value = await bcrypt.hash(password, 10);
collection.updateOne({ email }, [
{ $set: { "hash": hash_value } }
]);
我想{ email }
有同样的问题,一定很可能你正在寻找{ email: email }
看这个例子:
var hash1 = "bar"
var hash2 = "$bar"
db.collection.insertMany([{ _id: 1, hash1: "foo" }, { _id: 2, hash2: "foo" }])
db.collection.updateOne({ _id: 1 }, [{ $set: { hash1 } }]);
db.collection.updateOne({ _id: 2 }, [{ $set: { hash2 } }]);
db.collection.find()
{ "_id" : 1.0, "hash1" : "bar" },
{ "_id" : 2.0 }
在 Mongo 4.4 及更早版本$
中不允许使用,请参阅字段名称
顶级字段名称不能以美元符号 ($) 字符开头。
MongodB 5.0 添加了对在字段名称中使用 ( $
) 和 ( )的改进支持。.
有一些限制。有关详细信息,请参阅字段名称注意事项。
一般来说,我不建议在字段名称中使用 ( $
) 和 ( )。.
无论如何,动态字段名称很糟糕。
推荐阅读
- testflight - iTMSTransporter 将 ipa 上传到 iTunes Connect Success 但在 TestFlight 中选择 Failed
- python - 如何从条目(Tkinter)中获取值,在公式中使用它并将结果打印在标签中
- docker - 无法连接到位于 unix:///var/run/weave/weave.sock 的 Docker 守护程序。docker 守护进程是否正在运行?
- azure - 访问 Azure 存储库时出现 403 被禁止
- python - 具有不同深度的分层索引的 Pandas 数据框
- ios - 如何去除 UISearchbar 的边框?
- javascript - 如何仅在angular5的ngx数据表中选择当前页面的行
- java - 使用@Profile 决定执行测试类
- mariadb - 如何进行复制并仅修改 MariaDB 中的某些数据?
- html - 如何让 Flexbox 响应式响应?