C# driver 2.0 Mongodb UpdateOneAsync -
`
public class student { public long studentid {get; set;} public string fname {get; set;} public string lname {get; set;} public list<objectid> courseslist {get; set;} public int iq {get;set;} } public class courses { [bsonid] public objectid id { get; set; } public string coursenumber{get; set;} public string coursename{get; set;} }
`
how add/append courser id course list(which may null first time) of student object
ps: know how set field using below command. hoping on similar lines above problem
await studentcollection.updateoneasync(a => a.studentid == studentid, builders<student>.update.set( => a.iq,90));
as you've discovered, c# code use $addtoset is:
var filter = builders<student>.filter.eq(s => s.studentid, studentid); var update = builders<student>.update.addtoset(s => s.courseslist, courseid); var result = await collection.updateoneasync(filter, update);
however, $addtoset not going work if courselist member has been stored in collection null. server requires existing value $addtoset array (it can empty array).
the easiest solution store empty list courseslist instead of null when there no courses.
Comments
Post a Comment