【发布时间】:2017-01-26 18:08:17
【问题描述】:
我想重命名我们文档的 tags 数组中的标签,例如将集合中的所有标签a 更改为c。文件看起来像这样:
[ { _id: …, tags: ['a', 'b', 'c'] },
{ _id: …, tags: ['a', 'b'] },
{ _id: …, tags: ['b', 'c', 'd'] } ]
我需要保持标签唯一。这意味着,这样的更新将不起作用,因为第一个文档最终将包含标签 c 两次:
db.docs.update(
{ tags: 'a' },
{ $set: { 'tags.$': 'c' } }
)
所以,我也尝试了这个:
db.docs.update(
{ tags: 'a' },
{
$pull: { 'a' },
$addToSet: { 'c' }
}
)
但这给出了MongoError: Cannot update 'tags' and 'tags' at the same time。
是否有机会通过一次更新重命名标签?
【问题讨论】:
标签: mongodb