猫鼬查找和更新子文档

时间:2020-10-22 05:47:04

标签: node.js mongoose

我有以下架构:

const FinanceSchema = new mongoose.Schema({
  moneyToBePaid: {
    type: Number,
  },

  moneyPaid: {
    type: Number,
  },

  moneyToBeReceived: {
    type: Number,
  },

  moneyReceived: {
    type: Number,
  },
});

const UserSchema = new mongoose.Schema({
  firstname: {
    type: String,
    required: true,
  },
  lastname: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  phoneNo: {
    type: Number,
    required: true,
  },
  financialInformation: [FinanceSchema],
});

因此,对于每个用户,我都可以拥有尽可能多的financialInformation。 我能够将financialInformation保存在用户集合中,但无法更新financialInformation记录。

例如,假设我有2条FinancialInformation记录:

"firstname":"John",
...................................... ,
"financialInformation":[{
"_id": "5f9116c70785f818e8404f9ff",
moneyToBePaid: 200,

  moneyPaid:300,

  moneyToBeReceived:400,

  moneyReceived:500
},{
"_id": "5f9116c70785f818e8404f9e"
moneyToBePaid: 100,

  moneyPaid:100,

  moneyToBeReceived:100,

  moneyReceived:100
}]

现在我要更新第一个FinancialInfo。

<-这是我的路线,我不确定路线,我想更新用户的特定financialInfo->

router.patch("/users/:user_id/profile/:finance_id", async (req, res) => {
  const finance_id = req.params.finance_id;
  const user_id = req.params.user_id;

  try {

   }

<-我是否需要创建财务收款系统? ->

0 个答案:

没有答案