Mongodb : 使用 node.js 猫鼬将新数据对象添加到对象数组中
Mongodb : Adding new data object into an array of object with node.js mongoose
我想在 MongoDB 的嵌套文档中添加一个新的数据对象。这就是我的模式:
const transactionSchema = new Schema({
transactionType: {type: String, require: true, trim: true},
senderWallet: {type: String, require: true, trim: true},
recipientWallet: {type: String, require: true, trim: true},
amount: {type: Number, require: true, trim: true},
createdAt: {type: Date, require: true},
});
const walletHoldShema = new Schema({
balance: {type: Number},
transaction:[transactionSchema]
});
const walletSchema = new Schema({
createdAt: {type: Date, require: true},
userId: {type: String, require: true, trim: true},
walletAdrress: {type: String},
usdWallet: [walletHoldShema],
})
const Wallets = mongoose.model('wallet', walletSchema);
module.exports = Wallets
这就是我的数据现在的样子
wallet: {
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction": []
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
}
我尝试过不同的解决方案,例如我通过 遵循的解决方案:
const _id = req.params._id;
const transactionInput = {
transactionType: 'input',
senderWallet: req.body.sender,
recipientWallet: req.body.recipient,
amount: req.body.amount,
createdAt: Date.now(),
};
walletSchema.findOneAndUpdate({_id: _id}, {$push: {transaction: transactionInput}}, { new: true }, function(err, data){
if (err) {
res.send(err);
} else {
res.send(transactionInput);
}
});
// calling router
router.patch('/v1/sendUSD/', wallet.sendDollar
但是在我的路由器被调用后,我在我的数据库中得到了这个:
"wallet": [
{
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction": []
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
},
{
"_id": "60ca5e8e2c5746373",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"transaction":
[
{
"transactionType": "input",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
}
]
}
],
},
]
但我想要的是这个:
wallet: [
{
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction":
[
{
"transactionType": "in",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
},
{
"transactionType": "out",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
},
]
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
},
]
在数组中包含数组总是一个坏主意。也就是说,您可以通过以下方式将交易推送到 usdWallet:
walletSchema.findOneAndUpdate({ _id: _id }, { $push: { "usdWallet.$.transaction": transactionInput }}, { new: true }, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(transactionInput);
}
});
我想在 MongoDB 的嵌套文档中添加一个新的数据对象。这就是我的模式:
const transactionSchema = new Schema({
transactionType: {type: String, require: true, trim: true},
senderWallet: {type: String, require: true, trim: true},
recipientWallet: {type: String, require: true, trim: true},
amount: {type: Number, require: true, trim: true},
createdAt: {type: Date, require: true},
});
const walletHoldShema = new Schema({
balance: {type: Number},
transaction:[transactionSchema]
});
const walletSchema = new Schema({
createdAt: {type: Date, require: true},
userId: {type: String, require: true, trim: true},
walletAdrress: {type: String},
usdWallet: [walletHoldShema],
})
const Wallets = mongoose.model('wallet', walletSchema);
module.exports = Wallets
这就是我的数据现在的样子
wallet: {
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction": []
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
}
我尝试过不同的解决方案,例如我通过
const _id = req.params._id;
const transactionInput = {
transactionType: 'input',
senderWallet: req.body.sender,
recipientWallet: req.body.recipient,
amount: req.body.amount,
createdAt: Date.now(),
};
walletSchema.findOneAndUpdate({_id: _id}, {$push: {transaction: transactionInput}}, { new: true }, function(err, data){
if (err) {
res.send(err);
} else {
res.send(transactionInput);
}
});
// calling router
router.patch('/v1/sendUSD/', wallet.sendDollar
但是在我的路由器被调用后,我在我的数据库中得到了这个:
"wallet": [
{
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction": []
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
},
{
"_id": "60ca5e8e2c5746373",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"transaction":
[
{
"transactionType": "input",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
}
]
}
],
},
]
但我想要的是这个:
wallet: [
{
"_id": "60ca5e8e2c57463733b65f89",
"usdWallet": [
{
"_id": "60ca5ede2c57463733b65f8a",
"balance": 0,
"transaction":
[
{
"transactionType": "in",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
},
{
"transactionType": "out",
"senderWallet": "fcdhjgknsdfihber",
"recipientWallet": "mbindu",
"amount": "50.0",
"createdAt": 1623881009306
},
]
}
],
"createdAt": "2021-06-16T20:28:14.589Z",
"userId": "fb0df5c9a9eb",
"walletAdrress": "fb0df5c9a9eb",
"__v": 0
},
]
在数组中包含数组总是一个坏主意。也就是说,您可以通过以下方式将交易推送到 usdWallet:
walletSchema.findOneAndUpdate({ _id: _id }, { $push: { "usdWallet.$.transaction": transactionInput }}, { new: true }, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(transactionInput);
}
});