I have below data:
[
{
"_id": {
"$oid": "6459f1020162e7e3649729c9"
},
"user_id": {
"$oid": "644fd02b7add0a888e2e8e84"
},
"creditTotal": 500,
"debitTotal": 0,
"creditDebitTotal": 500,
"userWallet": [
{
"userName": "hhjj",
"mobileNumber": "6666699888",
"sumTotal": 500,
"debit": {
"totalDebit": 0,
"transactions": []
},
"credit": {
"totalCredit": 500,
"transactions": [
{
"amount": 500,
"date": "2023-05-17",
"description": "didi",
"_id": {
"$oid": "64644941a6a3a7abdbf90316"
}
}
]
},
"_id": {
"$oid": "6462f70a2ff97a1eac2290a8"
}
}
]
},
{
"_id": {
"$oid": "6459f1020162e7e3649729ce"
},
"user_id": {
"$oid": "6459ecf2dcb04e1d845bb6f2"
},
"creditTotal": 2500,
"debitTotal": 1000,
"creditDebitTotal": 1500,
"userWallet": [
{
"userName": "h",
"mobileNumber": "22222222222",
"sumTotal": 1500,
"debit": {
"totalDebit": 1000,
"transactions": [
{
"amount": 1000,
"date": "2023-05-16",
"description": "rt",
"_id": {
"$oid": "64635ad2a6a3a7abdbf8f4b1"
}
}
]
},
"credit": {
"totalCredit": 2500,
"transactions": [
{
"amount": 500,
"date": "2023-05-16",
"description": "ft",
"_id": {
"$oid": "646359e3a6a3a7abdbf8f4ac"
}
},
{
"amount": 2000,
"date": "2023-05-16",
"description": "rt",
"_id": {
"$oid": "64635ba6a6a3a7abdbf8f4c7"
}
}
]
},
"_id": {
"$oid": "64630eb92ff97a1eac229517"
}
}
]
}
]
I want to update an element in transaction with the element id as input.
Input:
{
"user_id": "6459ecf2dcb04e1d845bb6f2", // user_id for level 1 match
"walletId": "64630eb92ff97a1eac229517", // for userWallet object match
"transId": "646359e3a6a3a7abdbf8f4ac", // transaction array object match
"amount": "5000", // updated amount
"type": "Credit" // userWallet credit or debit category check
}
I want to update object with user_id
== "644fd02b7add0a888e2e8e84"
as below:
{
"_id": {
"$oid": "6459f1020162e7e3649729ce"
},
"user_id": {
"$oid": "6459ecf2dcb04e1d845bb6f2"
},
"creditTotal": 7000,
"debitTotal": 1000,
"creditDebitTotal": 6000,
"userWallet": [
{
"userName": "h",
"mobileNumber": "22222222222",
"sumTotal": 6000,
"debit": {
"totalDebit": 1000,
"transactions": [
{
"amount": 1000,
"date": "2023-05-16",
"description": "rt",
"_id": {
"$oid": "64635ad2a6a3a7abdbf8f4b1"
}
}
]
},
"credit": {
"totalCredit": 7000,
"transactions": [
{
"amount": 5000,
"date": "2023-05-16",
"description": "ft",
"_id": {
"$oid": "646359e3a6a3a7abdbf8f4ac"
}
},
{
"amount": 2000,
"date": "2023-05-16",
"description": "rt",
"_id": {
"$oid": "64635ba6a6a3a7abdbf8f4c7"
}
}
]
},
"_id": {
"$oid": "64630eb92ff97a1eac229517"
}
}
]
}
I have tried multiple ways but could not reach to the level 2 filter.
const filter = {
"user_id": userId,
"userWallet._id": id,
"userWallet.credit.transactions._id": transId
};
const update = {
$set: {
"userWallet.$.credit.transactions.$[transaction].amount": amount,
}
};
const arrayFilters = [
{ "transaction._id": transId }
];
const updateOptions = {
returnOriginal: false
};
var updatedDocument = await mongoose.connection.collections.wallets.collection.findOneAndUpdate(filter, update, { arrayFilters, ...updateOptions });
2
Answers
Tty this:
const arrayFilters = [
{ "transaction._id": transId,
"user_id": userId
}
];
array of filter documents that determine which array elements to modify for an update operation on an array field.https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndUpdate/
Here’s one way you could do it using two
"arrayFilters"
.Try it on mongoplayground.net.