I have a table to store users with records like this.
{
"_id" : "",
"Username" : "",
"Points" : 0,
"CompletedAchievements" : {
"Public": [],
"Private": []
},
"ActiveAchievements" : {
"Public": [],
"Private": []
},
"Kudos" : [],
"Teams" : [],
"UserType" : "Admin",
"PrivateAchievements": []
}
My C# code
public void Add<T>(string userId, string achievementId, int progress) {
var collection = this.db.GetCollection<T>(typeof(T).Name);
var filter = Builders<T>.Filter.And(
Builders<T>.Filter.Eq("_id", userId),
Builders<T>.Filter.Eq("ActiveAchievements.Public", "Public"));
var update = Builders<T>.Update.Push("ActiveAchievements.$.Public", new
ActiveAchievement() { _id = achievementId, Progress = progress });
collection.FindOneAndUpdateAsync(filter, update);
}
I am trying to add a value to the Public Array in active achievements. Does anyone know why this isn't working?
Was a simple fix in the end. Only 1 filter required the _id filter to get the record.
var filter = Builders<T>Filter.Eq("_id", userId);
Once I had that I could do the following update onto the array on the object.
var update = Builders<T>.Update.Push("ActiveAchievements.Public", new
ActiveAchievement() { _id = achievementId, Progress = progress });