0

I have a List that looks something like:

[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"

Where the structure of the class is like this:

public class Student 
{
  public int GroupId { get; set; }
  public string Subject { get; set; }
}

With my Replace method, I am passing in a GroupId. For the passed in GroupId, I want to replace all Student instances with new Students.

Right now, my Replace is not doing anything. I inspected the Collections before my attempt to Replace and after, and the Lists are the same. I am not too familiar with MongoDriver, but it doesn't seem like there is a ReplaceMany? This is my attempt:

 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();
}

When calling the method:

ReplaceStudents(1, Database);
  • If you interrogated the returned ReplaceOneResult from the ReplaceOneAsync call, you should find the reason why it's failing. – Danielle Summers Jun 13 '18 at 14:13
  • Was getting MongoBulkWriteException`1: A bulk write operation resulted in one or more errors. After applying the update, the (immutable) field '_id' was found to have been altered to _id: BinData(3, –  Jun 13 '18 at 14:16
  • Yup, that's what I'd expect. You're re-placing the document ID when you create a new student - which isn't what should happen with a ReplaceOne() call. – Danielle Summers Jun 13 '18 at 14:20
  • Is this an UpdateMany then? For some reason, I can't find sufficient documentation on how to do replace w/ new Objects. –  Jun 13 '18 at 14:22
  • There isn't an UpdateMany and it wouldn't help in this scenario. What you need to do is delete and create a new student, OR create a new student with the same _id as a student you want to replace. – Danielle Summers Jun 13 '18 at 14:57
  • Cool, that worked. Thank you! –  Jun 13 '18 at 15:12
  • You actually have the "terminology" incorrect here. What you are looking for with a `Replace` is actually an `Update`, and in fact `UpdateMany`. Both are actually the same API operator anyway at a base level, but the difference is `Replace` replaces the "whole object" being both the `GroupId` and `Subject` of the document. `UpdateMany` on the other hand allows you to set `"Database"` for every matching document to a `GroupId` with a single database operation using `$set`. Which is what you were really asking to do, but got caught up on the word "replace". – Neil Lunn Jun 13 '18 at 21:56
  • So, the delete and then insert is not correct? How would this work with new Objects? And why was this marked as duplicate? This is written in C#, not Mongo. –  Jun 14 '18 at 13:22

0 Answers0