我的列表看起来像:
[0] Student - GroupId: 1, Subject: "History"
[1] Student - GroupId: 1, Subject: "Math"
[2] Student - GroupId: 1, Subject: "Art"
[3] Student - GroupId: 2, Subject: "Spanish"
[4] Student - GroupId: 2, Subject: "Gym"
[5] Student - GroupId: 2, Subject: "English"
[6] Student - GroupId: 3, Subject: "Science"
[7] Student - GroupId: 3, Subject: "Science"
[8] Student - GroupId: 3, Subject: "Reading"
类的结构如下:
public class Student
{
public int GroupId { get; set; }
public string Subject { get; set; }
}
使用我的Replace方法,我传递的是GroupId。对于传入的GroupId,我想用新学生替换所有学生实例。
现在,我的替换没有做任何事情。在我尝试替换之前和之后,我检查了收集,并且列表是相同的。我对MongoDriver不太熟悉,但它似乎没有一个ReplaceMany?这是我的尝试:
public static void ReplaceStudents(int GroupId, IMongoDatabase Database)
{
IMongoCollection<Node> collection = Database.GetCollection<Student>("Students");
List<Student> studentObjects= collection.Find(s => true).ToList();
List<Student> before = collection.Find(s => true).ToList();
var filter = Builders<Student>.Filter.Eq(n => n.GroupId, GroupId);
collection.ReplaceOneAsync(filter, new Student());
List<Student> after = collection.Find(s => true).ToList();
}
调用方法时:
ReplaceStudents(1, Database);