![](/img/trans.png)
[英]Add new field To collection in MongoDB but the value based on value in another field
[英]Update a Collection to add a new Field with the value of another Field of same Collection in bulk mood
我对 mongoDB 有点陌生。 假设我有一个具有如下结构的集合:
mycoll
{
_id,
col1 -> NumberInt
}
集合中已经有数千份文档。 现在,我想更新集合的每个文档以添加 NumberInt 类型的新字段(即col2 ),其值将与字段col1的值相同。 有没有什么办法可以批量处理,而不是为每个文档运行更新命令? 我目前正在使用 4.4.1 版本的 mongoDB
您可以使用 Mongo updateMany() 功能。
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
检查此链接以获取更多详细信息, 更新许多文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.