要更新MongoDB文档中的单个字段,最好的方法是什么?在feathersjs中更新或修补钩子

问题描述 投票:2回答:1

我试图在单个字段中更新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: []
  }
};
javascript node.js mongodb feathersjs feathers-hook
1个回答
1
投票

update将取代整个文件。要与现有数据合并,应使用patch。这是记录herehere与以下示例:

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);
© www.soinside.com 2019 - 2024. All rights reserved.