MongoDB: Editing Element In Array -
i wanting edit element might exist in multiple arrays in collection.
public class class { [bsonid] public guid id { get; set;} public string name {get; set;} public list<student> students {get; set;} } public class student { [bsonid] public guid id {get; set;} public string name {get; set;} public string grade {get; set;} }
then class collection like
{ "_id" : nuuid("..."), "name" : "computer science", "students" : [ { "_id" : nuuid("..."), "name" : "chris" "grade" : "a" }, { "_id" : nuuid("..."), "name" : "bob" "grade" : "b" } }
and student collection like
{ "_id" : nuuid("..."), "name" : "chris eastwood" "grade": "c }
now when student updates information want information updated in each class.
i trying do:
// given student has been edited var query = query.eq("students._id", student.id); var update = update<class> .pull(c => c.students, x => x.eq(q => q.id, student.id)) .push(c => c.students, student) context.class.update(query,update,updateflags.multi);
but not work since "cannot update students , students @ same time"
i wondering there way update student in each array each class contains student?
thanks!
i'm not familiar c# driver, query looking for:
db.classes.update({ 'students._id': some_student_id, }, { $set: { 'students.$.property': some_value } },{ multi: true });
the key concept looking positional $ operator. index of object matched in query.