[英]How to use feathers mongoose query update with $not and $elemMatch
我正在嘗試通過服務器端使用feathersjs方法更新來更新我的文檔,如下所示:
context.app.service('myDocumens').update({
_id: obj._id,
'data': {
"$not": {
"$elemMatch": {
'year': 2018
}
}
}
}, {
$addToSet: {
'data': {
'position': 'senior',
'group': 1,
'comment': "",
'year': 2018
}
}
}).then(result => {
console.log(result)
});}
我在下面的示例文檔中顯示:
{
"_id": ObjectId("...."),
"firstName": "Adam",
"lastName": "Brown",
"data": [
{ "position":"senior","group":1,"comment":"","year":2018 },
{ "position":"junior","group":0,"comment":"","year":2017 }
]
}
使用上面的查詢,我嘗試將新對象添加到數組中,並包含一個字段“ year”:2018(如果此數組中不存在該字段,但是在對數組運行此查詢后的所有時間都添加新對象,盡管該數組已經包含一個帶有字段的對象'年':2018。 怎么了?
當我在mongoDb shell中運行以上查詢時,它工作正常。
我注意到的主要問題是您錯誤地使用了feathersjs adapter.update API。 對文檔ID的引用應在對象之外。 嘗試切換到此簽名:
context.app.service('myDocumens').update(id, data, params);
有關更多信息,請參見API文檔。 https://docs.feathersjs.com/api/databases/common.html#adapterupdateid-data-params
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.