如何将一个嵌套数据与模式中的其他数据进行比较?

时间:2019-05-29 05:33:56

标签: node.js express mongoose

好吧,我仍然对猫鼬和快递的工作一无所知。我关心的是如何将此嵌套数据(serialno)与新数据(serial no)进行比较,我需要访问嵌套的productrefno(serialno)和products(serialno)以便对其进行分类,并告诉用户不要输入再次使用相同的serialno,Schema如下所示。

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const AMCRegistrationSchema = new Schema({
  amcrefno: { type: String },
  amcregdate: { type: Date },
  customer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customer"
  },
  customertype: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customertype"
  },
  department: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customersubdepartment"
  },
  serviceprovider: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "serviceprovider"
  },
  amcstartdate: { type: Date },
  amcexpiredate: { type: Date },
  productrefno: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "proreg"
  },
  products: [
    {
      oem: {
        type: Schema.Types.ObjectId,
        ref: "company"
      },
      category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "productcategory"
      },
      subcategory: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "productsubcategory"
      },
      modelno: {
        type: String
      },
      serialno: {
        type: String
      }
    }
  ],
  remarks: { type: String },
  entrydate: {
    type: Date,
    dafault: Date.now
  }
});
module.exports = AMCRegistration = mongoose.model(
  "amcregistration",
  AMCRegistrationSchema
);

填充后的结果

[
{
"_id": "5ced9d065fbad80d40d48800",
"amcrefno": "AMC564",
"amcregdate": "2019-08-09T00:00:00.000Z",
"customer": {
"_id": "5cd4bf61738bce0df05b636d",
"customername": "NERIE"
},
"customertype": {
"_id": "5c7a1a1d4913fa08ac75c027",
"customertype": "Government "
},
"department": {
"_id": "5cbd67c709aeca1ea480157a",
"department": "Hardware"
},
"serviceprovider": {
"_id": "5ca1a5649dc41928180c5909",
"providername": "Computer Store"
},
"amcstartdate": "2019-08-09T00:00:00.000Z",
"amcexpiredate": "2019-08-09T00:00:00.000Z",
"productrefno": {
"_id": "5cec8e513f3de927a4d91aa2",
"refno1": "Ragahav123",
"refno2": "Raghav567",
"date": "2019-12-12T00:00:00.000Z",
"customer": {
"_id": "5cd4c3c1738bce0df05b636e",
"customername": "Kygo Music",
"customertype": "5cd4cd828ef5b52a4c7bec88",
"contactno": 7897897989,
"alternatecontactno": 1231231231,
"email": "nice@gmail.com",
"fax": 111111111111111,
"address": "Mawlai Iewrynghep",
"date": "2019-05-10T00:20:17.493Z",
"__v": 0
},
"customertype": {
"_id": "5c98b23f14ca7d1dac7d6ecc",
"customertype": "Individual",
"description": "Individual Description",
"__v": 0
},
"department": {
"_id": "5c88a0062a612422c087e201",
"department": "Accounts",
"customer": "5c889f4a2a612422c087e200",
"description": "Cisco Systems, Inc. is an American multinational technology conglomerate headquartered in San Jose, California, in the center of Silicon Valley, that develops, manufactures and sells networking hardware, telecommunications equipment and other high-technology services and products.",
"date": "2019-03-13T06:15:34.072Z",
"__v": 0
},
"products": [
{
"warrantyfrom": "2019-05-24T00:00:00.000Z",
"warrantyto": "2019-05-16T00:00:00.000Z",
"oemwarrantyfrom": "2019-05-29T00:00:00.000Z",
"oemwarrantyto": "2019-05-29T00:00:00.000Z",
"_id": "5cec8e733f3de927a4d91aa4",
"oem": {
"_id": "5cb6dc624708df233c4afd31",
"companyname": "Tesla",
"contactperson": "Elon Musk",
"contactno": 7878787878,
"alternatecontactno": 9696969696,
"email": "tesla@gmail.com",
"fax": 56565656,
"address": "New York",
"date": "2019-04-17T07:57:22.960Z",
"__v": 0
},
"category": {
"_id": "5ca35cf59ccd6a20d82419f4",
"category": "Saddam",
"description": "Lorem Ipsum",
"__v": 0
},
"subcategory": {
"_id": "5cb818725588431cd0ec3ceb",
"subcategory": "Monitor",
"parentcategory": "5c889bae492da521909b7a1a",
"description": "Lorem Ipsum",
"__v": 0
},
"modelno": "M1234",
"serialno": "567"
},
{
"warrantyfrom": "2019-05-24T00:00:00.000Z",
"warrantyto": "2019-05-16T00:00:00.000Z",
"oemwarrantyfrom": "2019-05-29T00:00:00.000Z",
"oemwarrantyto": "2019-05-29T00:00:00.000Z",
"_id": "5cec8e7a3f3de927a4d91aa5",
"oem": {
"_id": "5cb6dc624708df233c4afd31",
"companyname": "Tesla",
"contactperson": "Elon Musk",
"contactno": 7878787878,
"alternatecontactno": 9696969696,
"email": "tesla@gmail.com",
"fax": 56565656,
"address": "New York",
"date": "2019-04-17T07:57:22.960Z",
"__v": 0
},
"category": {
"_id": "5ca35cf59ccd6a20d82419f4",
"category": "Saddam",
"description": "Lorem Ipsum",
"__v": 0
},
"subcategory": {
"_id": "5cb818725588431cd0ec3ceb",
"subcategory": "Monitor",
"parentcategory": "5c889bae492da521909b7a1a",
"description": "Lorem Ipsum",
"__v": 0
},
"modelno": "M1234",
"serialno": "896"
}
],
"__v": 2
},
"remarks": "Lorem Ipsum",
"products": [
{
"_id": "5cedb69e2366d920e4c836c7",
"oem": {
"_id": "5c9b68412746d02fe4e7dea6",
"companyname": "HP"
},
"category": {
"_id": "5c889bae492da521909b7a1a",
"category": "Desktop Update"
},
"subcategory": {
"_id": "5ca35fbed6e1430df88954a6",
"subcategory": "Keyboard"
},
"modelno": "AM123",
"serialno": "AS123"
},
{
"_id": "5cedb70d2366d920e4c836c9",
"oem": {
"_id": "5cb6dc624708df233c4afd31",
"companyname": "Tesla"
},
"category": {
"_id": "5ca35cf59ccd6a20d82419f4",
"category": "Saddam"
},
"subcategory": {
"_id": "5cb818725588431cd0ec3ceb",
"subcategory": "Monitor"
},
"modelno": "AM567",
"serialno": "AS567"
}
],
"__v": 8
}
]

如何在路线上做到这一点?

router.post("/checkserialno", (req, res) => {


 AMCReg.find().then(amc => {
    // const productrefno = req.body.proregid;
    // const products = req.body.products;

    const productrefno = {
      serialno: req.body.serialno
    };

    const products = {
      serialno: req.body.serialno
    };

    if (productrefno !== products) {
      res.json({ msg: "true" });
    } else {
      res.json({ msg: "false" });
    }
  });

});

我可以填充它并显示这些数据

router.get("/compareserialno", (req, res) => {


 AMCReg.find()
    .select("amcrefno")
    //SERIAL NO of the PRODUCTREFNO 
    .populate({
      path: "productrefno",
      select: "products.serialno"
    })

    //SERIAL NO of the AMC product
    .select("products.serialno")
    .then(amc => res.json(amc))
    .catch(err => res.json(err));
});

0 个答案:

没有答案