Search code examples
mongodbmongodb-.net-driver

Insert value to existing collection in Mongo DB


I have a Mongo DB table Notes with following structure

Id(autoincrement value)
UserId
Notes(Collection)

And now i need to add one more record into that existing Notes collection. How do i do it in Mongo DB using Mongo DB driver?

In my repository i tried something like following, but its not working

public NoteUser AddNote(string userId, Note note)
{
    var filters = Builders<NoteUser>.Filter.Eq(x=>x.UserId, userId);
    context.Notes.InsertOne(filter,note);
}

How do i insert a new record to existing collection in Mongo DB table?


Solution

  • Finally i got the answer which i was looking for. for achieving above i need to write something similar to this

    studentsCol.UpdateOneAsync(
    Builders<Student>.Filter.Eq(x => x.Id, studentId),
    Builders<Student>.Update.Push(x => x.MarkList, newMark));