10

I would like to update a field in an array of embedded documents.

My sample document is:

db.students.insert(
 {
 first_name:'AA',
 last_name:'BB',
 cours_reussis:[{intitule:'PAI',note:0,name_school:'EPFC'}]
 }
)

I'd like to change the value of name_school to "ENSA" instead of "EPFC".

Stennie
10.4k2 gold badges31 silver badges47 bronze badges
asked Dec 4, 2016 at 20:47
0

4 Answers 4

4

Try this. It may need small modifications based on your other variables but I think this will work:

db.students.update(
 { first_name: 'AA' },
 { $set:
 {
 "cours_reussis.0.name_school": 'ENSA'
 }
 }
)

cours_reussis is an array. The 0 is the array index.

For your reference: $set

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
answered Dec 4, 2016 at 21:22
0
19

The answer from Kalhara is correct if you know the position of the embedded document in your array, however there is an alternative approach that can be used to update the first matching array element without knowing the position.

Assuming you update with a single array in your query criteria, the positional operator ($) can be used to refer to the matching array element:

db.students.update(
 // Match criteria
 {
 first_name: 'AA',
 last_name: 'BB',
 'cours_reussis.name_school': 'EPFC'
 },
 // Update first matching array element using the positional operator ($)
 {
 $set: {
 'cours_reussis.$.name_school': 'ENSA',
 }
 }
)
answered Dec 4, 2016 at 23:07
3
  • 1
    Thank you very much Stennie Unfortunately it does not work. It gives me this message WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: cours_reussis.$.nom_ecole" } }) Sorry for my english i use google to translate Commented Dec 5, 2016 at 13:36
  • 1
    @zouhair The WriiteResult indicates that a matching document was not found, so you need to adjust your criteria. My update statement works for your sample document as included in the question description, but you'll have to modify to suit your current data. It looks like perhaps you need to change name_school in the update statement to nom_ecole to match your actual data, Commented Dec 5, 2016 at 19:28
  • 1
    this should be marked as the right answer Commented Aug 27, 2018 at 14:11
0

You can use this to update the first matching array element without knowing the index:

db.students.update(
 { first_name: 'AA',name_school:'EPFC' },
 { $set:
 {
 "cours_reussis.$.name_school": 'ENSA'
 }
 }
 )
kevinskio
4,2821 gold badge31 silver badges50 bronze badges
answered Jun 17, 2019 at 13:45
0

You can use this to update the array element with some conditions and without knowing the index:

db.getCollection('profiles').update(
 {
 'userId':'4360a380-1540-45d9-b902-200f2d346263',
 'skills.name':'css'
 },
 {
 $set: {'skills.$.proficiencyLevel': 5}
 }, 
 {
 multi: true
 }
)
answered Jun 16, 2020 at 17:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.