如何在MongoDB中创建自定义的增量ID或数字?

时间:2018-11-17 15:58:08

标签: javascript mongodb mongoose

const mongoose = require("mongoose");

const bills = new mongoose.Schema({
  companyId: mongoose.Schema.Types.ObjectId,
  billNo: Number,
  partyName: String,
  billedProduct: [{
    productName: String,
    price: Number,
    quantity: Number
  }],
  billDate: {
    type: Date,
  },
});


module.exports = mongoose.model("Bills", bills);

    export function addBill(req, res) {
  const { partyName, product, price, quantity } = req.body;

  var billObj = {
    companyId: req.user._id,
    billedProduct: [{
      product,
      price,
      quantity,
    }],
    partyName,
    billDate: Date.now(),
  };
  var newBill = new Bills(billObj);
  newBill
    .save()
    .then(bill => {
      console.log(bill);
      return res.json(bill);
    })
    .catch(err => {
      return res.json(err);
    });
}

下面是我要在每个新帐单上保存billNo递增1的模式吗?

现在在保存或创建新账单时在addBill功能中,我想对每一个新创建的账单将No都增加1

0 个答案:

没有答案