简体   繁体   English

在一个猫鼬命令中更新多个文档

[英]update multiple documents in single mongoose command

I have the following collection 我有以下收藏

[ { a : 'One', b : 'Two' }, { a : 'Three', b : 'Four' } ]

I want to update the collection in single query such that i will append substring 'numbered' to each of the 'a' property of each document in collection. 我想在单个查询中更新集合,这样我会将子串“编号”附加到集合中每个文档的“ a”属性中。

resultant collection should be : 结果集合应为:

[ { a : 'Onenumberd', b : 'Two' }, { a : 'Threenumbered', b : 'Four' } ]

How can i achieve the same in single mongoose query? 如何在单个猫鼬查询中实现相同的目的?

It is not currently possible to reference the existing value of any field when performing an update(). 当前无法在执行update()时引用任何字段的现有值。 You could find open JIRA ticket https://jira.mongodb.org/browse/SERVER-1765 您可以找到JIRA公开票https://jira.mongodb.org/browse/SERVER-1765

So only way for now is to query and then update using retrieved data. 因此,目前唯一的方法是使用检索到的数据进行查询然后进行更新。

You should use the $concat operator from the aggregation framework. 您应该使用aggregation框架中的$ concat运算符。 The code should be quite similar in mongoose as well. 该代码在猫鼬中也应该非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM