private bool TryBuildMap(MethodCallExpression node, out BsonValue result) { result = null; var sourceSerializationExpression = node.Arguments[0] as ISerializationExpression; if (sourceSerializationExpression != null) { var lambda = MongoExpressionVisitor.GetLambda(node.Arguments[1]); if (lambda.Body is ISerializationExpression) { result = BuildValue(lambda.Body); return(true); } var inputValue = BuildValue(node.Arguments[0]); var asValue = lambda.Parameters[0].Name; // HACK: need to add a leading $ sign to the replacement because of how we resolve values. var body = FieldNameReplacer.Replace(lambda.Body, sourceSerializationExpression.SerializationInfo.ElementName, "$" + asValue); var inValue = BuildValue(body); result = new BsonDocument("$map", new BsonDocument { { "input", inputValue }, { "as", asValue }, { "in", inValue } }); return(true); } return(false); }
private BsonValue BuildMethodCall(MethodCallExpression node) { BsonValue result; if (MongoExpressionVisitor.IsLinqMethod(node) && TryBuildLinqMethodCall(node, out result)) { return(result); } if (node.Object != null && node.Object.Type == typeof(string) && TryBuildStringMethodCall(node, out result)) { return(result); } if (node.Object != null && node.Object.Type.IsGenericType && node.Object.Type.GetGenericTypeDefinition() == typeof(HashSet <>) && TryBuildHashSetMethodCall(node, out result)) { return(result); } if (node.Object != null && node.Method.Name == "CompareTo" && (TypeHelper.ImplementsInterface(node.Object.Type, typeof(IComparable <>)) || TypeHelper.ImplementsInterface(node.Object.Type, typeof(IComparable)))) { return(new BsonDocument("$cmp", new BsonArray(new[] { BuildValue(node.Object), BuildValue(node.Arguments[0]) }))); } if (node.Object != null && node.Method.Name == "Equals" && node.Arguments.Count == 1) { return(new BsonDocument("$eq", new BsonArray(new[] { BuildValue(node.Object), BuildValue(node.Arguments[0]) }))); } var message = string.Format("{0} of type {1} is an unsupported method in a $project or $group pipeline operator.", node.Method.Name, node.Method.DeclaringType); throw new NotSupportedException(message); }
private BsonValue BuildMethodCall(MethodCallExpression node) { if (MongoExpressionVisitor.IsLinqMethod(node, "Select")) { var serializationExpression = node.Arguments[0] as IBsonSerializationInfoExpression; if (serializationExpression != null) { var body = MongoExpressionVisitor.GetLambda(node.Arguments[1]).Body; return(ResolveValue(body)); } } if (node.Type == typeof(string)) { return(BuildStringMethodCall(node)); } var message = string.Format("{0} is an unsupported method in a $project or $group pipeline operator.", node.Method.Name); throw new NotSupportedException(message); }