I am trying to updated this mongdb nested array using mongoose but no luck I put all the versions that is tried may be you can connect the dots I am using findOneAndUpdate is it even possible to update items this deep ?.
// Input from Front-End taking in to account that all the ids are static for now to simplyfy thigs
{Name : Jess}
// Function
const updateDeptArr = await Data.findOneAndUpdate(
// First Version
{
'_id':'60cbbb23d5741b51f8fefc2b' // First user ID
[`peeps._id`]: '60cbbb28d5741b51f8fefc2e', // Second ID 1st nest
[`arraNames._id`]: '60cbc2d1cf7b1c3250e08dc2' // Third ID 2nd nest
},
{
$set: {
[`peeps.$.arraNames.$.Name`]: 'Jess'
}, { _id: true, new: true })
// Second Version
{
'_id': '60cbbb23d5741b51f8fefc2b'
'peeps': {
"$elemMatch": {
"_id": '60cbbb28d5741b51f8fefc2e', "arraNames._id": "60cbc2d1cf7b1c3250e08dc2"
}
}
},
{
$set: {
// Test 1 [`peeps.$[outer].arraNames.$[inner].Name`]: 'Jess'
//Test 2 'peeps.$[outer].arraNames.$[inner].Name': 'Jess'
}
}, { _id: true, new: true })
// 50% working version Not dynamic but it modify the targeted element
const updateDeptArr = await Data.findOneAndUpdate(
{
'_id': req.body.userId, // user ID
[`peeps._id`]: '60cbbb28d5741b51f8fefc2e', // Second ID
},
{
$set: {
[`peeps.0.arraNames.0.Name`]: 'Jess'
}
}, { _id: true, new: true }
)
// Schema
{
UserId, '60cbbb23d5741b51f8fefc2b'
User: "",
peeps [{
_id: '60cbbb28d5741b51f8fefc2e'
arraNames: [{
_id:'60cbc2d1cf7b1c3250e08dc2'
Name: "",// End point Name must be Jess
}],
}],
I want that Name to be Jess to modify the end point, there are be many names