要更新 MongoDB 文档中的单个字段,最好的方法是什么? feathersjs 中的更新或补丁挂钩
To update single field in a document on MongoDB, what is the best method? update or patch hook in feathersjs
我尝试在单个字段中更新 mongodb 文档,我不确定我想使用哪种方法使用补丁或使用 feathers 框架更新,请举例说明我们如何做到这一点。
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 and 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);
我尝试在单个字段中更新 mongodb 文档,我不确定我想使用哪种方法使用补丁或使用 feathers 框架更新,请举例说明我们如何做到这一点。
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 and 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);