Node.js Mongoose查找和更新子文档
我有以下模式:Node.js Mongoose查找和更新子文档,node.js,mongoose,Node.js,Mongoose,我有以下模式: const FinanceSchema = new mongoose.Schema({ moneyToBePaid: { type: Number, }, moneyPaid: { type: Number, }, moneyToBeReceived: { type: Number, }, moneyReceived: { type: Number, }, }); const UserSchema = new
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],
});
因此,对于每个用户,我都可以获得尽可能多的财务信息。
我可以在用户集合中保存财务信息,但无法更新财务信息记录
假设我有两个财务信息记录:
"firstname":"John",
...................................... ,
"financialInformation":[{
"_id": "5f9116c70785f818e8404f9ff",
moneyToBePaid: 200,
moneyPaid:300,
moneyToBeReceived:400,
moneyReceived:500
},{
"_id": "5f9116c70785f818e8404f9e"
moneyToBePaid: 100,
moneyPaid:100,
moneyToBeReceived:100,
moneyReceived:100
}]
现在我想更新第一个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 {
}