[ad_1]
I am trying to figure out how to update Place “A” name and address here but when the request is made it never updates and i cannot figure out why?
Areas:
Areas is the parent document, when a place is created, it gets pushed into the proper areas Place array
[
{
_id: 1,
name: 'Name One',
places: [
{
place_id: 'a',
place_name: 'Place One',
place_address: 'Place Address One',
},
{
place_id: 'b',
place_name: 'Place Two',
place_address: 'Place Address Two',
},
{
place_id: 'c',
place_name: 'Place Three',
place_address: 'Place Address Three',
},
],
},
{...}.
]
Place: Child Document, when child document is updated, i am trying to update the parent documents field to reflect the proper data.
[
{
place_id: "a",
areaId: "AreaId",
areaName: "AreaName",
place_name: 'Place One',
place_address: 'Place Address One'
},
{
place_id: "c",
areaId: "AreaId",
areaName: "AreaName",
place_name: 'Place Two',
place_address: 'Place Address Two'
},
{
place_id: "d",
areaId: "AreaId",
areaName: "AreaName",
place_name: 'Place Three',
place_address: 'Place Address Three'
},
]
Place Creation: This properly works and pushes the new place into the Area.places array.
const createPlace = await Place.create({
areaId: req.body.id,
areaName: facilityDistrict.name,
place_name: req.body.name,
place_address: req.body.address,
})
//Adds created place to the proper area
await Area.updateOne(
{
_id: req.body.id,
},
{
$push: {
places: {
place_id: createPlace._id,
place_name: createPlace.name,
place_address: createPlace.address,
},
},
}
)
Update Place: this field properly works to update the place document
const updatePlace = await Place.updateOne(
{
_id: place._id,
},
{
$set: {
place_name: req.body.name,
place_address: req.body.address,
},
}
)
UpdateArea: This field does not work, i have tried multiple itterations of this but i cannot properly select the proper place document i am trying to update, ive tried adding filter arrays and still didnt work. I had a itteration where it did work but then updated all of the nested object documents to be the same information.
const updateArea = await Areas.updateOne(
{
_id: Areas._id,
places: {
$elemMatch: {
place_id: req.params.id,
},
},
},
{
$set: {
'places.$.place_name': req.body.name,
'places.$.place_address': req.body.address,
},
}
)
If someone could slap some advice for me id greatly appreciate it.
[ad_2]