I'm trying to update my hairdresser schema, and more precisely one object of my services array.
const hairdresserSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
email: { type: String, required: true },
password: { type: String, required: true },
label: { type: String, required: true },
description: { type: String, required: true },
stripe: { type: String, required: false },
phone: { type: String, required: true },
address: { type: String, required: true },
services: [
{
_id: { type: mongoose.Schema.Types.ObjectId },
label: { type: String },
service: [{ type: mongoose.Schema.Types.ObjectId, ref: "category" }],
price: { type: Number },
supplement: { type: Number },
duration: { type: Number }
}
]
});
const workerSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
label: { type: String, required: true },
entity: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "hairdresser" },
description: { type: String, required: true },
availability: [
{
id: { type: mongoose.Schema.Types.ObjectId, required: false },
date: { type: Date, required: false },
stops: [
{
stop: { type: String },
booked: { type: Boolean }
}
]
}
],
required: false
});
I tried to use $set and $ notation in order to target the desired object but Mongo keeps telling me that it's impossible to create a property.
worker.update(
{
'availability.stops._id': req.body.basicID
},
{
$set: {
'availability.stops.$.booked': true,
}
}
)
Here is a JSON formatted example of my worker schema :
{ _id: 5e4d6adbb12e8b5ccbb87d94,
label: 'Germain',
entity: 5e3d39fb0b40f96f98d33c1a,
description: 'Je prendrai soin de vos cheveux',
availability:
[ { stops: [{ _id: 5e4d6adbsdfds2e8b5ccbb87d75, stop: "10:00", booked: false }, { _id: 5e4d6adbsdfds2sdfd8b5ccbb87d75, stop: "10:15", booked: false }],
_id: 5e5edfd15605520adb7af977,
date: 2020-03-03T23:00:00.000Z },
{ stops: [{ _id: 5e4d6adbb12e8b5ccbb87d92, stop: "08:00", booked: false }, { _id: 5e4d6adbb12e8b5ccbb87d75, stop: "08:15", booked: false }],
_id: 5e5edfe05605520adb7af988,
date: 2020-03-03T23:00:00.000Z } ],
__v: 0 } ]
}
I need to upd Any idea how to make it work ?
You can use the filtered positional operator $ to update nested arrays.
router.put("/workers/:workerId/:stopId", async (req, res) => {
const result = await Worker.findByIdAndUpdate(
req.params.workerId,
{
$set: {
"availability.0.stops.$[stopId].booked": true
}
},
{
arrayFilters: [{ "stopId._id": req.params.stopId }],
new: true
}
);
res.send(result);
});
Let's say we have this document:
{
"_id": "5e5f8a2bf9f62d7558ad3be7",
"label": "Germain",
"entity": "5e3d39fb0b40f96f98d33c1a",
"description": "Je prendrai soin de vos cheveux",
"availability": [
{
"_id": "5e5f8a2bf9f62d7558ad3beb",
"stops": [
{
"_id": "5e5f8a2bf9f62d7558ad3bed",
"stop": "10:00",
"booked": false
},
{
"_id": "5e5f8a2bf9f62d7558ad3bec",
"stop": "10:15",
"booked": false
}
],
"date": "2020-03-03T23:00:00.000Z"
},
{
"_id": "5e5f8a2bf9f62d7558ad3be8",
"stops": [
{
"_id": "5e5f8a2bf9f62d7558ad3bea",
"stop": "08:00",
"booked": false
},
{
"_id": "5e5f8a2bf9f62d7558ad3be9",
"stop": "08:15",
"booked": false
}
],
"date": "2020-03-03T23:00:00.000Z"
}
],
"__v": 0
}
If we want to update the stop with _id 5e5f8a2bf9f62d7558ad3bec
for this document with _id: "5e5f8a2bf9f62d7558ad3be7"
, we send a PUT request http://.../workers/5e5f8a2bf9f62d7558ad3be7/5e5f8a2bf9f62d7558ad3bec
The result will be:
{
"_id": "5e5f8a2bf9f62d7558ad3be7",
"label": "Germain",
"entity": "5e3d39fb0b40f96f98d33c1a",
"description": "Je prendrai soin de vos cheveux",
"availability": [
{
"stops": [
{
"_id": "5e5f8a2bf9f62d7558ad3bed",
"stop": "10:00",
"booked": false
},
{
"_id": "5e5f8a2bf9f62d7558ad3bec",
"stop": "10:15",
"booked": true => UPDATED
}
],
"_id": "5e5f8a2bf9f62d7558ad3beb",
"date": "2020-03-03T23:00:00.000Z"
},
{
"stops": [
{
"_id": "5e5f8a2bf9f62d7558ad3bea",
"stop": "08:00",
"booked": false
},
{
"_id": "5e5f8a2bf9f62d7558ad3be9",
"stop": "08:15",
"booked": false
}
],
"_id": "5e5f8a2bf9f62d7558ad3be8",
"date": "2020-03-03T23:00:00.000Z"
}
],
"__v": 0
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加