private static void DoPush(BsonDocument doc, string name, BsonValue value) { var arrayNode = BsonHelpers.GetDocumentValue(name, doc); var isEach = value.IsBsonDocument && value.AsBsonDocument.First().Name == "$each"; BsonArray arrayValues; if (isEach) { arrayValues = value.AsBsonDocument.First().Value.AsBsonArray; } else { arrayValues = BsonArray.Create(new BsonValue[] { value }); } if (arrayNode == BsonNull.Value) { BsonHelpers.SetDocumentValue(name, doc, arrayValues, isUnset: false); return; } if (!arrayNode.IsBsonArray) { throw new InMemoryDatabaseException($"Cannot push to element [{arrayNode.BsonType.ToString()}] '{name}', it is not an array"); } var node = arrayNode.AsBsonArray; node.AddRange(arrayValues); }
private static void DoSet(BsonDocument doc, string name, BsonValue value) { BsonHelpers.SetDocumentValue(name, doc, value, isUnset: false); }