Skip to content

Instantly share code, notes, and snippets.

View dlee148's full-sized avatar
🚮

Daniel Lee dlee148

🚮
View GitHub Profile
@dlee148
dlee148 / positionalOperator.cs
Last active May 13, 2020 15:52
Workaround for updating nested objects using the MongoDB C# driver. The driver does not support multiple positional operators in one statement, making multi-level schema tedious to use.
public static void EditNested(string collectionName, string primaryKey, string secondaryKey, string tertiaryKey,
ArrayList parameters, PropertyInfo[] fields)
{
var collection = Mongo.Connect(out _client, out _database, collectionName);
var filter = Builders<BsonDocument>.Filter.Eq(someField, primaryKey);
var result = BsonSerializer.Deserialize<SomeType>(collection.Find(filter).First());
int index1 = 0, index2 = 0;
for (int i = 0; i < result.EmbeddedArray.Length; i++)
{