在猫鼬中从多个对象引用单个嵌入式对象

时间:2019-06-01 08:07:06

标签: node.js express mongoose

这是我使用猫鼬保存的数据结构,我只想保存一个数据。在产品字段下。

当我参考主要_id保存时,它会显示所有包含2个对象的产品。

 [
    {
    "_id": "5cf22b99f319fd34dce2fa80",
    "installrefno": "install Ref no 111",
    "installdate": "2019-06-01T00:00:00.000Z",
    "serialno": {
    "_id": "5cf10fcc4cb6352abcfe094a",
    "refno1": "test2",
    "refno2": "testRef2",
    "date": "2019-05-02T00:00:00.000Z",

    "products": [
                {
                  "warrantyfrom": "2019-05-19T00:00:00.000Z",
                  "warrantyto": "2019-05-14T00:00:00.000Z",
                  "oemwarrantyfrom": "2019-05-24T00:00:00.000Z",
                  "oemwarrantyto": "2019-05-25T00:00:00.000Z",
                  "_id": "5cf10fea4cb6352abcfe094b",
                  "oem": "5cb6e026042460131454cf45",
                  "category": "5c960902e5cf3429e06beb6c",
                  "subcategory": "5ca35fbed6e1430df88954a6",
                  "modelno": "A123888",
                  "serialno": "A345"
                  },
                  {
                  "warrantyfrom": "2019-05-19T00:00:00.000Z",
                  "warrantyto": "2019-05-14T00:00:00.000Z",
                  "oemwarrantyfrom": "2019-05-24T00:00:00.000Z",
                  "oemwarrantyto": "2019-05-25T00:00:00.000Z",
                  "_id": "5cf10ffb4cb6352abcfe094c",
                  "oem": "5c986a1e9b6bc614b8a551b9",
                  "category": "5c960902e5cf3429e06beb6c",
                  "subcategory": "5ca35fbed6e1430df88954a6",
                  "modelno": "QW123",
                  "serialno": "B345"
                  }
               ],
    "__v": 2
    },
    "contactperson": "raghav",
    "contactno": 345345,
    "address": "sdfasdf",
    "remarks": "adsfasdf",
    "installdoc": "client\\public\\install\\docs\\installdoc- 
     1559374745886.txt",
    "__v": 0
    },
    ]

这是架构,我已经从另一个文档proreg

中引用了serialno
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const InstallationSchema = new Schema({
  installrefno: { type: String },
  installdate: { type: Date },
  serialno: {
    type: mongoose.Schema.Types.ObjectId, 
    ref: "proreg"
  },
  installedby: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "employees"
  },

  customer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customer"
  },
  customertype: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customertype"
  },
  department: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customersubdepartment"
  },
  contactperson: { type: String },
  contactno: { type: Number },
  address: { type: String },
  remarks: { type: String },
  filename: {type: String},
  installdoc: { type: String}
});

module.exports = InstallationDetails = mongoose.model(
  "installation",
  InstallationSchema
);

这是我的添加路线,当我添加数据时,我在serialno字段中同时获得了这两种产品。

router.post("/add", upload.single("installdoc"), (req, res, next) => {
  console.log(req.file);
  const newInstallationDetails = new InstallationDetails({
    installrefno: req.body.installrefno,
    installdate: req.body.installdate,
    serialno: req.body.productregistrationid,
    installedby: req.body.employeesid,
    customer: req.body.customerid,
    customertype: req.body.customertypeid,
    department: req.body.customersubdepartmentid,
    contactperson: req.body.contactperson,
    contactno: req.body.contactno,
    address: req.body.address,
    remarks: req.body.remarks,
    installdoc: req.file.path
  });
  newInstallationDetails.save().then(installation => res.json(installation));
});

保存数据时如何只获得一种产品。有什么办法可以保存两个参考ID,即主要参考ID和产品ID。产品ID是serialno的子文档,它是proreg的引用。

0 个答案:

没有答案