MongoDB - 更新集合中的所有文档,使用文档中已存在的另一个字段的值添加新字段
我有一个由这样的文档组成的MongoDB集合:
{
title: "Foo",
subtitle: "Bar"
}
由于开发的需要,我想批量上传所有文档,为每个文档添加一个title_lower
键,这是保存为的现有文档标题小写字符串:
{
title: "Foo",
subtitle: "Bar",
title_lower: "foo"
}
我使用 Node.js 服务器和 Mongoose 与数据库进行通信,但我想避免在服务器上编写一次性方法来执行此操作。
我已经下载了 MongoCompass,希望 MongoSH(shell)可以帮助我实现这一目标,但我在重用现有文档值时遇到问题,无法 $set 一个新的 title_lower
值。
这可能吗?
I have a MongoDB collection consisting of such documents:
{
title: "Foo",
subtitle: "Bar"
}
Due to development necessities, I want to bulk upload all the documents, adding a title_lower
key to each of these documents, which is the existing document title saved as a lower-case string:
{
title: "Foo",
subtitle: "Bar",
title_lower: "foo"
}
I'm using a Node.js server and Mongoose to communicate with the database, but I would like to avoid writing a one-time method on the server to do this operation.
I have downloaded MongoCompass hoping the MongoSH (shell) could help me achieve this, but I'm having trouble reusing the existing document values, to $set a new title_lower
one.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 mongodb compass shell (_MONGOSH) 中运行查询,
我尝试过,它在我这边工作得很好,但我建议在更新您的真实集合之前在测试集合上尝试它。
You can run query in mongodb compass shell (_MONGOSH)
I tried and it works fine on my end, but I would suggest to try it on a test collection before updating your real collection.