2016-09-18 64 views
0

我想更新消息具有特定ID并且用户标识位于收件人数组中的子文档的值。我想用指定的用户ID更新匹配对象的值。FeathersJS Mongoose:更新匹配的子文档无法正常工作

当我上运行的MongoDB CLI下面的查询,一切正常和值更新:

db.getCollection('messages').update({ 
    _id : ObjectId("57d7edb8c497a75a6a7fde60"), 
    "recipients.userId" : "5789127ae2bcc79326462dbc" 
},{ 
    $set : {"recipients.$.read": true} 
}); 

但是当我运行通过JS以下查询在我FeathersJS应用:

messageService.update({ 
    _id : '57d7edb8c497a75a6a7fde60', 
    "recipients.userId" : "5789127ae2bcc79326462dbc" 
},{ 
    $set: {"recipients.$.read": true} 
}).then(function(e) { 
    console.log(e) 
}).catch(function(e) { 
    console.log(e); 
}); 

我得到错误:

GeneralError: The positional operator did not find the match needed from the query. Unexpanded update: recipients.$.read

我在做什么错?

是否有一个更好的方法来更新许多消息一次?

谢谢!

回答

1

对于由你需要通过设置idnull并把查询到params.query调用service method查询更新一个或多个记录:

messageService.update(null, { 
    $set: {"recipients.$.read": true} 
}, { 
    query: { 
    _id : '57d7edb8c497a75a6a7fde60', 
    "recipients.userId" : "5789127ae2bcc79326462dbc" 
    } 
}).then(function(e) { 
    console.log(e) 
}).catch(function(e) { 
    console.log(e); 
}); 
+0

有意义的我,但是当我运行这段代码,我得到一个“错误:[对象对象]”响应 – tan

+1

也许https://docs.feathersjs.com/debugging/readme.html可以帮助获得更好的错误信息(可能是它不能正确转换错误的错误)。 – Daff

+0

需要使用补丁而不是更新。谢谢! – tan