{
_id: ObjectId("65bc98f235375d1d97621e06"),
name: 'Aspirin',
type: 'Pain Relief',
forms: [
{
formType: 'TabletsData',
strength: '100mg',
dosageInstructions: [
{
time: 'Morning',
instructions: [
{
step: 1,
text: 'Take 1 tablet with breakfast.',
_id: ObjectId("65bc98f235375d1d97621e09")
},
{
step: 2,
text: 'Drink a full glass of water.',
_id: ObjectId("65bc98f235375d1d97621e0a")
}
],
_id: ObjectId("65bc98f235375d1d97621e08")
},
{
time: 'Evening',
instructions: [
{
step: 1,
text: 'Take 1 tablet with dinner.',
_id: ObjectId("65bc98f235375d1d97621e0c")
},
{
step: 2,
text: 'Avoid taking on an empty stomach.',
_id: ObjectId("65bc98f235375d1d97621e0d")
}
],
_id: ObjectId("65bc98f235375d1d97621e0b")
}
],
_id: ObjectId("65bc98f235375d1d97621e07")
},
{
formType: 'Capsule',
strength: '200mg',
dosageInstructions: [
{
time: 'Afternoon',
instructions: [
{
step: 1,
text: 'Take 1 capsule in the afternoon.',
_id: ObjectId("65bc98f235375d1d97621e10")
},
{
step: 2,
text: 'Take with or after food.',
_id: ObjectId("65bc98f235375d1d97621e11")
}
],
_id: ObjectId("65bc98f235375d1d97621e0f")
},
{
time: 'Night',
instructions: [
{
step: 1,
text: 'Take 1 capsule before bedtime.',
_id: ObjectId("65bc98f235375d1d97621e13")
},
{
step: 2,
text: 'Avoid taking with dairy products.',
_id: ObjectId("65bc98f235375d1d97621e14")
}
],
_id: ObjectId("65bc98f235375d1d97621e12")
}
],
_id: ObjectId("65bc98f235375d1d97621e0e")
}
],
ingredients: [
'Acetylsalicylic Acid',
'Inactive Ingredient 1',
'Inactive Ingredient 2'
],
__v: 0
}
2
Answers
You can use the
updateMany
method to change thetext
value of all matching documents.From your sample documents that match
step : 1
you have differenttext
for different times of the day. I presume you don’t want to update every singletext
to be the same value so here is an example that will set thetext
to"NEW VALUE"
only whenstep : 1
andtime: "Morning"
See HERE for a working example of
update
on mongoplayground.You have
__v
property which suggests mongoose. An example using mongoose would be:You can also update multiple nested array object elements conditionally in multiple documents via the following update/aggregation query ( mongoDB v.4.2+ ):
Explanation:
Playground