9
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 do I add/append a courser Id to Course list(which may be null for the first time) of a Student object

PS: I know how to set a field using the below command. I am hoping it is on similar lines for the above problem

await StudentCollection.UpdateOneAsync(a => a.StudentId == studentId, Builders<Student>.Update.Set( a => a.IQ,90));
3
  • You mean something like this: Builders<Student>.Update.Set( a => a.CourseList, new []{ObjectId}) Commented Apr 14, 2015 at 22:57
  • more like Builders<Student>.Update.AddToSet( a => a.CourseList, newCourse.Id}) where newCourse is the newly course object created and I am trying to append its course Id into that course list of student object Commented Apr 15, 2015 at 0:27
  • Builders<Student>.Update.AddToSet( a => a.CourseList, new []{newCourse.Id}) Commented Apr 15, 2015 at 0:44

3 Answers 3

17

As you've already discovered, the C# code to 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 is not going to work if the CourseList member has been stored in the collection as a null. The server requires that the existing value for $addToSet be an array (it can be an empty array).

The easiest solution is to just store an empty list for CoursesList instead of a null when there are no courses.

Sign up to request clarification or add additional context in comments.

1 Comment

storing empty list to start with then using Builders<Student>.Update.AddToSetEach(s => s.CoursesList, courseList); worked for me. I think i didn't mention clearly in my question that i wanted add/append a new course list created. Thanks !
0

This is working for me. When you define "List" like this, it will be empty and works with AddToSet/Push methods.

public List<ObjectId> CoursesList = new List<ObjectId>();

Comments

0

The only case that you have to pay attention is when the array CourseList is null, in this case you have to use the code below (see also here):

var newListOfObject = new List<ObjectId>(){...}
await StudentCollection.UpdateOneAsync(a => a.StudentId == studentId, Builders<Student>.Update.Set( a => a.CoursesList, newListOfObject));

Otherwise you can use AddToSet or Push like explain in the other answers.

Comments

Your Answer

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.