我的目标:我希望更新特定路径中集合中的多个文档,条件是路径与正则表达式匹配,然后搜索并替换路径中的某个值,最后将所有这些文档永久保存在db中。
示例:
myCollection : [
{ doc1 : { summary : 'Summary 1 : one', value : 1 },
{ doc2 : { summary : 'Summaryyuist 2 : two', value : 1 },
{ doc3 : { summary : 'hello 3 : three', value : 3 },
];
现在我想用路径中的'hello'替换所有'Summary':'summary'
所以查询后的结果应为:
myCollection : [
{ doc1 : { summary : 'hello 1 : one', value : 1 },
{ doc2 : { summary : 'helloyuist 2 : two', value : 1 },
{ doc3 : { summary : 'hello 3 : three', value : 3 },
];
我只是在查看上面要使用的查询。
从这里开始 http://mongoosejs.com/docs/api.html#query_Query-regex
我找不到如何实施的信息。特别是'Number'参数在regex方法中的作用。?
同样来自这里:
http://mongoosejs.com/docs/api.html#query_Query-regex 此外,我没有找到monsoose正则表达式的“显示代码”链接。有人至少可以回复mongoose正则表达式代码的链接。
我只找到如何查找模型,而不是如何使用与某些文档路径中的正则表达式匹配的值替换更新。
如何实现我的目标?
答案 0 :(得分:0)
你希望使用mongoose Model.Update选项。
Model.update = function (query, doc, options, callback) { ... }
示例:
MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
MyModel.update({ name: 'Tobi' }, { ferret: true }, { multi: true }, function(err,numberAffected, raw) {
if (err) return handleError(err);
console.log('The number of updated documents was %d', numberAffected);
console.log('The raw response from Mongo was ', raw);
});