使用猫鼬将文档从一个集合移动到另一个集合

时间:2019-07-26 05:51:59

标签: node.js express mongoose

我想创建一个功能,用户可以将文档从一个集合移动到另一个集合,但是我的功能无法正常工作。通常,它会覆盖不匹配的文档。

所以我有两个阶段 1)主要2)局部 因此,如果用户在主舞台上,然后单击“保存”,则应将其存储到局部文件并从Main中删除。但是它确实保存了部分文档,但是它也会覆盖主集合中的另一个文档,并且不会删除。

这是我的模型(主要和部分相同)

  extraRequirements: String,
    creator: String,
    customerID: {type:String, unique:true},
    userInformation: {
        userType: String,
        sender: {
            email: String,
            company: String
        },
        reciever: {
            email: String,
            company: String
        }
    },
    contactInformation: {
        name: String,
        phone: String,
        email: String,
        otherInformation: String
    },
    orderInformation: {
        orderNumber: String,
        customerOrderNumber: String
    },
    toolsList: [{
        name: String,
        toolType: String,
        serialNumber: String,
        materialType: String,
        customerDriveID: String,
        done: Boolean,
        progress: Number,
        MainParts: [{
            text: String,
            index: String,
            parts: String,
            status: String,
            MainTopics: [{
                id: String,
                text: String,
                topicTypes: String,
                comment: String,
                images: String,
                imageName: String,
                answers: String,
                fields: [{
                    fieldType: String,
                    text: String,
                    values: [String],
                    answer: String,
                    result: String,
                    additionalInput: Boolean,
                    subFields: [{
                        images: String,
                        imageName: String,
                        type: String
                    }]
                }]
            }]
        }],
    }]

和route.js

router.post('/stage/partials', (req, res) => {
  const partial = new PartialRequest({
    customerID: req.body.customerID,
    creator: req.body.creator,
    extraRequirements: req.body.extraRequirements,
    contactInformation: req.body.contactInformation,
    orderInformation: req.body.orderInformation,
    jobSiteInformation: req.body.jobSiteInformation,
    toolsList: req.body.toolsList
  });
  partial.save().then(result => {
    return res.status(200).json({
      message: 'Handling POST request to /partial',
      result: result
    });
  });
  Main.deleteOne({
    customerID: req.body.customerID
  }).then(data => {
    if (data) {
      console.log(data);
      return res.status(201).json(data);
    } else {
      return res.status(404).json({
        message: 'Error deleting request!'
      });
    }
  });
});

现在在Nodejs后端,它可以console.log正确的ID,但实际上它会删除错误的文档。 我使用了以下方法

findByIdAndRemove();
insertMany();

工具列表:它包含用户必须填写的调查表,因此如果一个用户填写了清单的一半,则他们会将其保存在(部分)中,当部分保存时,应从Main Stage中删除。

0 个答案:

没有答案