Reputation: 45
I have a collection that looks like this
{
"_id":ObjectId("58a5a40663d24e0498ecf5e1"),
"picture":null,
"close":{
"hour":19,
"minute":30
},
"open":{
"hour":7,
"minute":0
},
"location":{
"latitude":-6.304718,
"longitude":106.64337
},
"address":"abcd",
"placeName":"xyz",
"floor":[
{
"number":1,
"slot":[
{
"id":"A1",
"availability":true
},
{
"id":"A2",
"availability":true
},
{
"id":"A3",
"availability":true
},
{
"id":"A4",
"availability":true
},
{
"id":"A5",
"availability":true
},
{
"id":"B1",
"availability":true
},
{
"id":"B2",
"availability":true
},
{
"id":"B3",
"availability":true
},
{
"id":"B4",
"availability":true
},
{
"id":"B5",
"availability":true
}
]
},
{
"number":3,
"slot":[
{
"id":"A1",
"availability":true
},
{
"id":"A2",
"availability":true
},
{
"id":"A3",
"availability":true
},
{
"id":"B4",
"availability":true
},
{
"id":"B5",
"availability":true
}
]
}
],
"__v":0
}
And now I want to update one of the 'availability' field which the slot.id matches the give query.
When I want to update with this code
ParkingPlace.findOneAndUpdate({
'_id': mongoose.Types.ObjectId(placeID),
'floor': {
$elemMatch: {
'number': body.floor
}
},
'floor.slot': {
$elemMatch: {
'id': body.slot
}
}
}, {
$set: {
'slot.$.availability': false
}
}, function (err, docs) {
if (err) {
res.json(err);
} else {
res.json(docs);
}
});
It returns the doc, which should mean it succeeds, but the 'availability' field in the database is still not updated.
Does anyone know why and how to fix it?
**Edit: What I mean is that the 'availability' field on the database is still set to true even though I already set it to false.
Upvotes: 0
Views: 780
Reputation: 5345
You could use the new
option:
new: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
ParkingPlace.findOneAndUpdate({
'_id': mongoose.Types.ObjectId(placeID),
'floor': {
$elemMatch: {
'number': body.floor
}
},
'floor.slot': {
$elemMatch: {
'id': body.slot
}
}
}, {
$set: {
'slot.$.availability': false
}
}, {
new: true
}, function (err, docs) {
if (err) {
res.json(err);
} else {
res.json(docs);
}
});
Upvotes: 1