【发布时间】:2019-09-26 15:40:25
【问题描述】:
我有这样的文件:
{
_id: 'some id',
body: 'i want some apple',
},
{
_id: 'some id2',
body: 'i want some apple and banana',
}
我想查找并替换所有文档的正文短语 some apple 到 lots of oranges。
预期结果:
{
_id: 'some id',
body: 'i want lots of oranges',
},
{
_id: 'some id2',
body: 'i want lots of oranges and banana',
}
所以我找到了所有的文件:
myDB.find({
"body": {
"$regex": "some apple",
"$options": "i"
}
},
function(err, docs) {
console.log(docs);
}
);
)
但不知道如何仅将文档的特定正文短语 some apple 替换和更新为 lots of oranges。
我该怎么做?
【问题讨论】:
-
如果您使用的是允许
aggregation pipeline更新的mongodb 4.2版本。 This does it
标签: node.js mongodb mongoose text-search