I am trying to actually replace a collection of Objects of type Game in my Collection "Games".
I want to replace these Objects with entirely new Objects. I have researched a bit on MongoDB and I see that 'UpdateMany' will replace Fields with new values but that's not exactly what I want. I wish to replace the entire Object.
For reference, this is my Game class:
public class Game
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Developer { get; set; }
public int ProjectId { get; set; }
public Game()
{
this.Id = Guid.NewGuid();
}
}
This is my method I am using to attempt a bulk Replace. I am passing in a ProjectId, so for all of the Game Objects that have a ProjectId = to the argument, replace the Object with a new Game Object.
public static void ReplaceGame(int ProjectId, IMongoDatabase Database)
{
IMongoCollection<Game> gameCollection = Database.GetCollection<Game>("Game");
List<Game> gameCollectionBeforeReplacement = gameCollection.Find(g => true).ToList();
if (gameCollectionBeforeReplacement.Count == 0)
{
Console.WriteLine("No Games in Collection...");
return;
}
var filter = Builders<Game>.Filter.Eq(g => g.ProjectId, ProjectId);
foreach (Game game in gameCollection.AsQueryable())
gameCollection.ReplaceOneASync(filter, new Game() { Title = "REPLACEMENT TITLE" });
}
Not only does this take an excessive amount of time. I suspect it's because of the .AsQueryable() call but it also doesn't work. I am wondering how I can actually replace all instances picked up by my filter with new Game Objects.