要更新MongoDB文档中的单个字段,最好的方法是什么?在feathersjs中更新或修补钩子
我试图在单个字段中更新mongodb文档,我怀疑我想使用哪个方法使用补丁或使用羽毛框架更新,给出一个示例我们如何做到这一点。
const { authenticate } = require('feathers-authentication').hooks;
module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
回答如下:
update
将取代整个文件。要与现有数据合并,应使用patch
。这是记录here和here与以下示例:
app.service('messages').patch(1, {
text: 'A patched message'
}).then(message => console.log(message));
const params = {
query: { read: false }
};
// Mark all unread messages as read
app.service('messages').patch(null, {
read: true
}, params);