【问题标题】:mongoose plugin doesn't hook on findOneAndUpdatemongoose 插件无法连接 findOneAndUpdate
【发布时间】:2021-07-07 01:11:51
【问题描述】:

我像这样设置了一个猫鼬模式:

const expertSchema = new mongoose.Schema({
  firstName: {
    type: String,
    required: true,
  },
  lastName: {
    type: String,
    required: true,
  },
  contactMethods: {
    type: [ContactMethod.schema],
  }
})

expertSchema.plugin(autoIncrement.plugin, {
  model: 'EXP-',
  startAt: 1,
  prefix: 'EXP-'
});

const Expert = mongoose.model('Expert', expertSchema);

const contactMethodsSchema = new mongoose.Schema({
  type: {
    type: String,
    required: true,
  },
  value: {
    type: String,
    required: true,
  },
  preferred: {
    type: Boolean
  }
});

contactMethodsSchema.plugin(autoIncrement.plugin, {
  model: 'CON-',
  startAt: 1,
  prefix: 'CON-'
});
const ContactMethod = mongoose.model('ContactMethod', contactMethodsSchema)

我正在使用mongoose-auto-increment插件来自动增加我的monogodb文档的id(实际上是使用this fork,但它们基本相同)

我有一些代码可以为我的开发提供种子。 db 在重启时看起来像这样:

const contMethod1 = new models.ContactMethod({
  type: 'email',
  value: "janedoe@acme.org",
  preferred: false,
});

const contMethod2 = new models.ContactMethod({
  type: 'phone',
  value: "+12125550711",
  preferred: true,
});

const expert1 = new models.Expert({
  firstName: 'Jane',
  lastName: 'Doe',
  contactMethods: [contMethod1, contMethod2]
});

expert1.save();

这段代码效果很好,我最终得到的文档如下所示:

{
  "_id": "EXP-1",
  "firstName": "Jane",
  "lastName": "Doe",
  "contactMethods": [{
      "type": "email",
      "value": "janedoe@acme.org",
      "preferred": false,
      "_id": "CON-1"
    },
    {
      "type": "phone",
      "value": "+12125550711",
      "preferred": true,
      "_id": "CON-2"
    }
  ]
}

但是,当我的前端将此编辑后的数据发回给我时,我得到的 json 格式如下:

{
  _id: "EXP-1",
  "contactMethods": [{
      "type": "email",
      "value": "janedoe@acme.org",
      "preferred": false,
      "_id": "CON-1"
    },
    {
      "type": "phone",
      "value": "+12125550711",
      "preferred": true,
      "_id": "CON-2"
    },
    {
      "type": "whatsapp",
      "value": "+123456789",
      "preferred": false
    }
  ]
}

现在我想要更新现有的嵌入式文档和/或在需要时添加新文档。我为此目的设置了这段代码(我很欣赏可能有一种更智能的方式来实现它,但即便如此,它还是有点“有效”):

req.body.contactMethods.map((_, index) => {
  if (_._id) {
    expert = req.context.models.Expert.findOneAndUpdate({
      "contactMethods._id": _._id
    }, {
      $set: {
        "contactMethods.$.type": _.type,
        "contactMethods.$.value": _.value,
        "contactMethods.$.preferred": _.preferred
      }
    }, {
      useFindAndModify: false,
      returnOriginal: false,
      runValidators: true
    })
  } else {
    expert = req.context.models.Expert.findOneAndUpdate({
      "_id": req.user._id,
    }, {
      $push: {
        "contactMethods": _
      }
    }, {
      useFindAndModify: false,
      returnOriginal: false,
      runValidators: true
    })
  }
})

不幸的是,在这种情况下,我的插件是否没有“触发”或调用,最终我在集合中更新了一个文档,该文档缺少新推送的 contactMethod 的“_id”属性。

为什么我的插件没有在 findOneAndUpdate 调用中被调用?

我找到了this question,但我的插件不使用 ES6 语法。我还找到了this comment,但当前的猫鼬文档不再说明这一点,所以我有点希望他们改变/修复它。

谢谢! (第一个问题,呸..)

【问题讨论】:

    标签: javascript node.js mongodb mongoose plugins


    【解决方案1】:

    我最终通过更改我的代码并将添加新的联系方法拆分为两个操作来解决(或者更确切地说是解决这个问题):findOne 和保存,如下所示:

    req.body.contactMethods.map((_, index) => {
      if (_._id) {
        expert = req.context.models.Expert.findOneAndUpdate({
          "contactMethods._id": _._id
        }, {
          $set: {
            "contactMethods.$.type": _.type,
            "contactMethods.$.value": _.value,
            "contactMethods.$.preferred": _.preferred
          }
        }, {
          useFindAndModify: false,
          returnOriginal: false,
          runValidators: true
        })
      } else {
        expert = req.context.models.Expert.findOne({
          "_id": req.user._id,
        })
        expert.contactMethods.push(_);
        expert.save({ validateModifiedOnly: true });
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 2018-08-01
      • 2020-05-05
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多