如何根据链接的文档字段对字段进行更新

时间:2019-06-27 09:28:05

标签: mongodb

我试图有条件地更新文档中的一个字段,但是我需要知道链接文档中另一个字段的值。我正在努力解释我的问题,所以我会给你我的代码,并尝试解释我要做什么。

我有一条更新“工作站”系列的路线

router.post("/workstation/update/:id", (req, res, next) => {
  const id = req.params.id;

  const previousWorkstation = req.body.previous;
  const updatedWorkstation = req.body.updated;

  const {errors, isValid} = validateWorkstation(updatedWorkstation);
  if(!isValid){
    return res.status(422).json(errors);
  }

  Workstation.findByIdAndUpdate(mongoose.Types.ObjectId(id), updatedWorkstation, (err, workstation) => {
    if(err) return;
    NonConformity.updateMany({workstation: previousWorkstation.name, line: previousWorkstation.line}, {$set:{workstation: updatedWorkstation.name}}, (err) => {
      if(err) return;
      Rework.updateMany({workstation: previousWorkstation.name}, {$set:{workstation: updatedWorkstation.name}}, (err) => {
        if(err) return;
        res.send(200);
      })
    })
  })
});

我的问题是我还需要根据行来更新返工,而不仅仅是工作站名称!这是我的模式

// WORKSTATION SCHEMA

const mongoose = require('mongoose');

const WorkstationSchema = mongoose.Schema({
  name: {type:String, required: true},
  line: {type:String, required: true}
})

const Workstation = mongoose.model('workstations', WorkstationSchema);

module.exports = Workstation;

// REWORK SCHEMA 

const mongoose = require('mongoose');

const ReworkSchema = mongoose.Schema({
  nonConformity_id: {type:String, required: true},
  operator: {type:String, required: true},
  fin: {type:String, required: true},
  workstation: {type:String, required: false},
  code: {type:String, required: true},
  declination: {type:String, required: true},
  description: {type:String, required: true},
  advice: {type:String, required: false},
  pps: {type: String, required: false},
  quantity: {type:Number, required: true},
  reworked: {type:Boolean, default: false, required: true},
  verified: {type:Boolean, default: false, required: true},
})

const Rework = mongoose.model('nonConformities.reworks', ReworkSchema);

module.exports = Rework;

// NON CONFORMITY 

const mongoose = require('mongoose');

const NonConformitySchema = mongoose.Schema({
  number: {type: String, unique: true, required: true, dropDups: true},
  line: {type: String, required: true},
  product: {type: String, required: true},
  workstation: {type: String, required: true},
  msn: {type: String, required: true},
  date: {type: String, required: true},
  type: {type: String, required: true},
})

const NonConformity = mongoose.model('nonConformities', NonConformitySchema);

module.exports = NonConformity;

我的问题是:是否可以根据返工方案中链接的不合格ID的行名来更新返工集合中的工作站名称?

对不起,如果我的英语不好,如果您不明白我的问题,我可以尝试重新制定。

谢谢!

0 个答案:

没有答案