将值插入Mongo DB中的现有集合

时间:2018-12-08 05:15:33

标签: mongodb mongodb-.net-driver

我有一个具有以下结构的Mongo数据库表说明

Id(autoincrement value)
UserId
Notes(Collection)

现在我需要在现有的Notes集合中再添加一条记录。我如何使用Mongo DB驱动程序在Mongo DB中做到这一点?

在我的存储库中,我尝试了以下操作,但操作不正常

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

如何在Mongo DB表中的现有集合中插入新记录?

1 个答案:

答案 0 :(得分:1)

最后,我得到了我一直在寻找的答案。为了实现上述目标,我需要写 与此类似的东西

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