/// <summary>
        /// Sets the name and value of the additional field.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TMember">The type of the member.</typeparam>
        /// <param name="memberExpression">The member expression.</param>
        /// <param name="value">The value.</param>
        /// <returns>The args so calls can be chained.</returns>
        public GeoHaystackSearchArgs SetAdditionalField <TDocument, TMember>(Expression <Func <TDocument, TMember> > memberExpression, TMember value)
        {
            var serializationInfoHelper = new BsonSerializationInfoHelper();
            var serializationInfo       = serializationInfoHelper.GetSerializationInfo(memberExpression);
            var serializedValue         = serializationInfoHelper.SerializeValue(serializationInfo, value);

            SetAdditionalField(serializationInfo.ElementName, serializedValue);
            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Appends an unwind stage to the pipeline.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TNewResult">The type of the new result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="field">The field to unwind.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IAggregateFluent <TDocument, TNewResult> Unwind <TDocument, TResult, TNewResult>(this IAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, object> > field)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(field, "field");

            var helper = new BsonSerializationInfoHelper();

            helper.RegisterExpressionSerializer(field.Parameters[0], source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>());
            var serialiationInfo = helper.GetSerializationInfo(field.Body);

            return(source.Unwind <TNewResult>("$" + serialiationInfo.ElementName));
        }
Esempio n. 3
0
        public static BsonDocument FilterToBsonDocument <TDocument>(IBsonSerializerRegistry registry, object filter)
        {
            var filterExpr = filter as Expression <Func <TDocument, bool> >;

            if (filterExpr != null)
            {
                var helper = new BsonSerializationInfoHelper();
                helper.RegisterExpressionSerializer(filterExpr.Parameters[0], registry.GetSerializer <TDocument>());
                return(new QueryBuilder <TDocument>(helper).Where(filterExpr).ToBsonDocument());
            }

            return(ToBsonDocument(registry, filter));
        }
Esempio n. 4
0
        /// <summary>
        /// Appends a descending sort stage to the pipeline.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="field">The field to sort by.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IOrderedAggregateFluent <TDocument, TResult> SortByDescending <TDocument, TResult>(this IAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, object> > field)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(field, "field");

            var helper = new BsonSerializationInfoHelper();

            helper.RegisterExpressionSerializer(field.Parameters[0], source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>());
            var sortDocument = new SortByBuilder <TResult>(helper).Descending(field).ToBsonDocument();

            source = source.Sort(sortDocument);

            return(new AggregateFluent <TDocument, TResult>(source.Collection, source.Pipeline, source.Options, source.ResultSerializer));
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the distinct values for a specified field.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TField">The type of the result.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="field">The field.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The distinct values for the specified field.
        /// </returns>
        public static Task <IReadOnlyList <TField> > DistinctAsync <TDocument, TField>(this IMongoCollection <TDocument> collection, Expression <Func <TDocument, TField> > field, Expression <Func <TDocument, bool> > filter, DistinctOptions <TField> options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(collection, "collection");
            Ensure.IsNotNull(filter, "filter");

            var helper = new BsonSerializationInfoHelper();

            helper.RegisterExpressionSerializer(field.Parameters[0], collection.Settings.SerializerRegistry.GetSerializer <TDocument>());

            var serializationInfo = helper.GetSerializationInfo(field.Body);

            options = options ?? new DistinctOptions <TField>();
            if (options.ResultSerializer == null)
            {
                options.ResultSerializer = (IBsonSerializer <TField>)serializationInfo.Serializer;
            }
            return(collection.DistinctAsync(serializationInfo.ElementName, filter, options, cancellationToken));
        }
Esempio n. 6
0
        /// <summary>
        /// Modifies the current sort stage by appending a descending field specification to it.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="field">The field to sort by.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IOrderedAggregateFluent <TDocument, TResult> ThenByDescending <TDocument, TResult>(this IOrderedAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, object> > field)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(field, "field");

            var helper = new BsonSerializationInfoHelper();

            helper.RegisterExpressionSerializer(field.Parameters[0], source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>());
            var sortDocument = new SortByBuilder <TResult>(helper).Descending(field).ToBsonDocument();

            // this looks sketchy, but if we get here and this isn't true, then
            // someone is being a bad citizen.
            var currentSortStage = (BsonDocument)source.Pipeline.Last();

            currentSortStage["$sort"].AsBsonDocument.AddRange(sortDocument);

            return(source);
        }
Esempio n. 7
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the MongoLinqFindQuery class.
 /// </summary>
 /// <param name="collection">The collection being queried.</param>
 /// <param name="documentType">The document type.</param>
 public SelectQuery(MongoCollection collection, Type documentType)
     : base(collection, documentType)
 {
     _serializationInfoHelper = new BsonSerializationInfoHelper();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QueryBuilder&lt;TDocument&gt;"/> class.
 /// </summary>
 /// <param name="serializationInfoHelper">The serialization info helper.</param>
 internal QueryBuilder(BsonSerializationInfoHelper serializationInfoHelper)
 {
     _serializationInfoHelper = serializationInfoHelper;
     _predicateTranslator     = new PredicateTranslator(_serializationInfoHelper);
 }
Esempio n. 9
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="PushEachOptionsBuilder{TValue}" /> class.
 /// </summary>
 /// <param name="serializationInfoHelper">The serialization info helper.</param>
 internal PushEachOptionsBuilder(BsonSerializationInfoHelper serializationInfoHelper)
 {
     _serializationInfoHelper = serializationInfoHelper;
 }