Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 11 commits
  • 26 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 16, 2012
@craiggwilson craiggwilson refactored SelectQuery into BsonSerializationInfoHelper and Predicate…
…Translator
767f213
@craiggwilson craiggwilson Added typed query builders and reimagined untyped query builder. b818efa
@craiggwilson craiggwilson added the rest of the typed builders. ed8de1e
rstam Fixed warnings and fixed unit test that was being skipped. 46a8dd5
rstam Added license to some files. Standardized using statements. Removed a…
… few checks for null returned from GetSerializationInfo (which now throws an exception). Removed some dead files. Made all files in the Linq\Utils folder share the same namespace.
22856d4
rstam Change to GetItemSerializationInfo to properly throw exception if ser…
…ializationInfo parameter does not implement IBsonItemSerializationInfoProvider.
c1d7df7
rstam Changed IEnumerable<TMember> to IEnumerable<TValue> in typed builders…
…. Renamed some variables to more closely align with their type names. Replaced a few <V> with <TMember>.
a014fe5
Commits on May 17, 2012
rstam Added Replace method to Update<TDocument>. 4645f3b
rstam Renamed Func queryBuilder parameters to queryBuilderFunction. Renamed…
… lamda parameter from q to qb in query builder functions in unit tests.
1c494d8
rstam Added overload of Matches method to SimpleQueryBuilder and QueryBuild…
…er<TDocument> that takes a BsonRegularExpression argument.
1fc28c5
rstam Renamed SimpleQueryBuilder to UntypedQueryBuilder. 318d5ad
View
171 Driver/Builders/FieldsBuilder.cs
@@ -16,12 +16,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
+using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
@@ -177,4 +178,172 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
}
}
+
+ /// <summary>
+ /// A builder for specifying which fields of a document the server should return.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public static class Fields<TDocument>
+ {
+ // public static properties
+ /// <summary>
+ /// Gets a null value with a type of IMongoFields.
+ /// </summary>
+ public static IMongoFields Null
+ {
+ get { return null; }
+ }
+
+ // public static methods
+ /// <summary>
+ /// Adds one or more field names to be excluded from the results.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new FieldsBuilder<TDocument>().Exclude(memberExpressions);
+ }
+
+ /// <summary>
+ /// Adds one or more field names to be included in the results.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new FieldsBuilder<TDocument>().Include(memberExpressions);
+ }
+
+ /// <summary>
+ /// Adds a slice to be included in the results.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="size">The size of the slice (negative sizes are taken from the end).</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static FieldsBuilder<TDocument> Slice<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, int size)
+ {
+ return new FieldsBuilder<TDocument>().Slice(memberExpression, size);
+ }
+
+ /// <summary>
+ /// Adds a slice to be included in the results.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="skip">The number of values to skip.</param>
+ /// <param name="limit">The number of values to extract.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static FieldsBuilder<TDocument> Slice<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, int skip, int limit)
+ {
+ return new FieldsBuilder<TDocument>().Slice(memberExpression, skip, limit);
+ }
+ }
+
+ /// <summary>
+ /// A builder for specifying which fields of a document the server should return.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ [Serializable]
+ public class FieldsBuilder<TDocument> : BuilderBase, IMongoFields
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private FieldsBuilder _fieldsBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the FieldsBuilder class.
+ /// </summary>
+ public FieldsBuilder()
+ {
+ _serializationInfoHelper = new BsonSerializationInfoHelper();
+ _fieldsBuilder = new FieldsBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Adds one or more field names to be excluded from the results.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ var elementNames = GetElementNames(memberExpressions);
+ _fieldsBuilder = _fieldsBuilder.Exclude(elementNames.ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Adds one or more field names to be included in the results.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ var elementNames = GetElementNames(memberExpressions);
+ _fieldsBuilder = _fieldsBuilder.Include(elementNames.ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Adds a slice to be included in the results.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="size">The size of the slice (negative sizes are taken from the end).</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public FieldsBuilder<TDocument> Slice<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, int size)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ _fieldsBuilder = _fieldsBuilder.Slice(serializationInfo.ElementName, size);
+ return this;
+ }
+
+ /// <summary>
+ /// Adds a slice to be included in the results.
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="skip">The number of values to skip.</param>
+ /// <param name="limit">The number of values to extract.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public FieldsBuilder<TDocument> Slice<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, int skip, int limit)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ _fieldsBuilder = _fieldsBuilder.Slice(serializationInfo.ElementName, skip, limit);
+ return this;
+ }
+
+ /// <summary>
+ /// Converts this object to a BsonDocument.
+ /// </summary>
+ /// <returns>A BsonDocument.</returns>
+ public override BsonDocument ToBsonDocument()
+ {
+ return _fieldsBuilder.ToBsonDocument();
+ }
+
+ // protected methods
+ /// <summary>
+ /// Serializes the result of the builder to a BsonWriter.
+ /// </summary>
+ /// <param name="bsonWriter">The writer.</param>
+ /// <param name="nominalType">The nominal type.</param>
+ /// <param name="options">The serialization options.</param>
+ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
+ {
+ ((IBsonSerializable)_fieldsBuilder).Serialize(bsonWriter, nominalType, options);
+ }
+
+ // private methods
+ private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions)
+ {
+ var elementNames = memberExpressions
+ .Select(x => _serializationInfoHelper.GetSerializationInfo(x))
+ .Select(x => x.ElementName);
+ return elementNames;
+ }
+ }
}
View
133 Driver/Builders/GeoHaystackSearchOptionsBuilder.cs
@@ -16,12 +16,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
+using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
@@ -146,4 +147,134 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
}
}
+
+ /// <summary>
+ /// A builder for the options of the GeoHaystackSearch command.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public static class GeoHaystackSearchOptions<TDocument>
+ {
+ // public static properties
+ /// <summary>
+ /// Gets a null value with a type of IMongoGeoHaystackSearchOptions.
+ /// </summary>
+ public static IMongoGeoHaystackSearchOptions Null
+ {
+ get { return null; }
+ }
+
+ // public static methods
+ /// <summary>
+ /// Sets the maximum number of results to return.
+ /// </summary>
+ /// <param name="value">The maximum number of results to return.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static GeoHaystackSearchOptionsBuilder<TDocument> SetLimit(int value)
+ {
+ return new GeoHaystackSearchOptionsBuilder<TDocument>().SetLimit(value);
+ }
+
+ /// <summary>
+ /// Sets the max distance.
+ /// </summary>
+ /// <param name="value">The max distance.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static GeoHaystackSearchOptionsBuilder<TDocument> SetMaxDistance(double value)
+ {
+ return new GeoHaystackSearchOptionsBuilder<TDocument>().SetMaxDistance(value);
+ }
+
+ /// <summary>
+ /// Sets the query on the optional additional field.
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="value">The value fo the additional field.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static GeoHaystackSearchOptionsBuilder<TDocument> SetQuery<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ return new GeoHaystackSearchOptionsBuilder<TDocument>().SetQuery(memberExpression, value);
+ }
+ }
+
+ /// <summary>
+ /// A builder for the options of the GeoHaystackSearch command.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ [Serializable]
+ public class GeoHaystackSearchOptionsBuilder<TDocument> : BuilderBase, IMongoGeoHaystackSearchOptions
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private GeoHaystackSearchOptionsBuilder _geoHaystackBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the GeoHaystackSearchOptionsBuilder class.
+ /// </summary>
+ public GeoHaystackSearchOptionsBuilder()
+ {
+ _serializationInfoHelper = new BsonSerializationInfoHelper();
+ _geoHaystackBuilder = new GeoHaystackSearchOptionsBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Sets the maximum number of results to return.
+ /// </summary>
+ /// <param name="value">The maximum number of results to return.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public GeoHaystackSearchOptionsBuilder<TDocument> SetLimit(int value)
+ {
+ _geoHaystackBuilder = _geoHaystackBuilder.SetLimit(value);
+ return this;
+ }
+
+ /// <summary>
+ /// Sets the max distance.
+ /// </summary>
+ /// <param name="value">The max distance.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public GeoHaystackSearchOptionsBuilder<TDocument> SetMaxDistance(double value)
+ {
+ _geoHaystackBuilder = _geoHaystackBuilder.SetMaxDistance(value);
+ return this;
+ }
+
+ /// <summary>
+ /// Sets the query on the optional additional field.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="value">The value fo the additional field.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public GeoHaystackSearchOptionsBuilder<TDocument> SetQuery<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ _geoHaystackBuilder = _geoHaystackBuilder.SetQuery(serializationInfo.ElementName, serializedValue);
+ return this;
+ }
+
+ /// <summary>
+ /// Converts this object to a BsonDocument.
+ /// </summary>
+ /// <returns>A BsonDocument.</returns>
+ public override BsonDocument ToBsonDocument()
+ {
+ return _geoHaystackBuilder.ToBsonDocument();
+ }
+
+ // protected methods
+ /// <summary>
+ /// Serializes the result of the builder to a BsonWriter.
+ /// </summary>
+ /// <param name="bsonWriter">The writer.</param>
+ /// <param name="nominalType">The nominal type.</param>
+ /// <param name="options">The serialization options.</param>
+ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
+ {
+ ((IBsonSerializable)_geoHaystackBuilder).Serialize(bsonWriter, nominalType, options);
+ }
+ }
}
View
103 Driver/Builders/GroupByBuilder.cs
@@ -16,12 +16,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
+using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
@@ -63,19 +64,39 @@ public class GroupByBuilder : BuilderBase, IMongoGroupBy
// constructors
/// <summary>
+ /// Initializes a new instance of the <see cref="GroupByBuilder"/> class.
+ /// </summary>
+ public GroupByBuilder()
+ {
+ _document = new BsonDocument();
+ }
+
+ /// <summary>
/// Initializes a new instance of the GroupByBuilder class.
/// </summary>
/// <param name="names">One or more key names.</param>
public GroupByBuilder(string[] names)
+ : this()
+ {
+ Keys(names);
+ }
+
+ // public methods
+ /// <summary>
+ /// Sets one or more key names.
+ /// </summary>
+ /// <param name="names">The names.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public GroupByBuilder Keys(params string[] names)
{
- _document = new BsonDocument();
foreach (var name in names)
{
_document.Add(name, 1);
}
+
+ return this;
}
- // public methods
/// <summary>
/// Returns the result of the builder as a BsonDocument.
/// </summary>
@@ -97,4 +118,80 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
}
}
+
+ /// <summary>
+ /// A builder for specifying what the GroupBy command should group by.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public static class GroupBy<TDocument>
+ {
+ // public static methods
+ /// <summary>
+ /// Sets one or more key names.
+ /// </summary>
+ /// <param name="memberExpressions">One or more key names.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public static GroupByBuilder<TDocument> Keys(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new GroupByBuilder<TDocument>().Keys(memberExpressions);
+ }
+ }
+
+ /// <summary>
+ /// A builder for specifying what the GroupBy command should group by.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public class GroupByBuilder<TDocument> : BuilderBase, IMongoGroupBy
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private GroupByBuilder _groupByBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the <see cref="GroupByBuilder&lt;TDocument&gt;"/> class.
+ /// </summary>
+ public GroupByBuilder()
+ {
+ _serializationInfoHelper = new BsonSerializationInfoHelper();
+ _groupByBuilder = new GroupByBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Sets one or more key names.
+ /// </summary>
+ /// <param name="memberExpressions">One or more key names.</param>
+ /// <returns>The builder (so method calls can be chained).</returns>
+ public GroupByBuilder<TDocument> Keys(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ var names = memberExpressions
+ .Select(x => _serializationInfoHelper.GetSerializationInfo(x))
+ .Select(x => x.ElementName);
+
+ _groupByBuilder = _groupByBuilder.Keys(names.ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Converts this object to a BsonDocument.
+ /// </summary>
+ /// <returns>A BsonDocument.</returns>
+ public override BsonDocument ToBsonDocument()
+ {
+ return _groupByBuilder.ToBsonDocument();
+ }
+
+ // protected methods
+ /// <summary>
+ /// Serializes the result of the builder to a BsonWriter.
+ /// </summary>
+ /// <param name="bsonWriter">The writer.</param>
+ /// <param name="nominalType">The nominal type.</param>
+ /// <param name="options">The serialization options.</param>
+ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
+ {
+ ((IBsonSerializable)_groupByBuilder).Serialize(bsonWriter, nominalType, options);
+ }
+ }
}
View
204 Driver/Builders/IndexKeysBuilder.cs
@@ -16,12 +16,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
+using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
@@ -185,4 +186,205 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
}
}
+
+ /// <summary>
+ /// A builder for specifying the keys for an index.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public static class IndexKeys<TDocument>
+ {
+ // public static methods
+ /// <summary>
+ /// Sets one or more key names to index in ascending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static IndexKeysBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new IndexKeysBuilder<TDocument>().Ascending(memberExpressions);
+ }
+
+ /// <summary>
+ /// Sets one or more key names to index in descending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static IndexKeysBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new IndexKeysBuilder<TDocument>().Descending(memberExpressions);
+ }
+
+ /// <summary>
+ /// Sets the key name to create a geospatial index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static IndexKeysBuilder<TDocument> GeoSpatial<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ return new IndexKeysBuilder<TDocument>().GeoSpatial(memberExpression);
+ }
+
+ /// <summary>
+ /// Sets the key name to create a geospatial haystack index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static IndexKeysBuilder<TDocument> GeoSpatialHaystack<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ return new IndexKeysBuilder<TDocument>().GeoSpatialHaystack(memberExpression);
+ }
+
+ /// <summary>
+ /// Sets the key name and additional field name to create a geospatial haystack index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <typeparam name="TAdditionalMember">The type of the additional member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="additionalMemberExpression">The additional member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static IndexKeysBuilder<TDocument> GeoSpatialHaystack<TMember, TAdditionalMember>(Expression<Func<TDocument, TMember>> memberExpression, Expression<Func<TDocument, TAdditionalMember>> additionalMemberExpression)
+ {
+ return new IndexKeysBuilder<TDocument>().GeoSpatialHaystack(memberExpression, additionalMemberExpression);
+ }
+ }
+
+ /// <summary>
+ /// A builder for specifying the keys for an index.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ [Serializable]
+ public class IndexKeysBuilder<TDocument> : BuilderBase, IMongoIndexKeys
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private IndexKeysBuilder _indexKeysBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the IndexKeysBuilder class.
+ /// </summary>
+ public IndexKeysBuilder()
+ {
+ _serializationInfoHelper = new BsonSerializationInfoHelper();
+ _indexKeysBuilder = new IndexKeysBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Sets one or more key names to index in ascending order.
+ /// </summary>
+ /// <param name="memberExpressions">One or more key names.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public IndexKeysBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ _indexKeysBuilder = _indexKeysBuilder.Ascending(GetElementNames(memberExpressions).ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Sets one or more key names to index in descending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public IndexKeysBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ _indexKeysBuilder = _indexKeysBuilder.Descending(GetElementNames(memberExpressions).ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Sets the key name to create a geospatial index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public IndexKeysBuilder<TDocument> GeoSpatial<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ _indexKeysBuilder = _indexKeysBuilder.GeoSpatial(serializationInfo.ElementName);
+ return this;
+ }
+
+ /// <summary>
+ /// Sets the key name to create a geospatial haystack index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public IndexKeysBuilder<TDocument> GeoSpatialHaystack<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ _indexKeysBuilder = _indexKeysBuilder.GeoSpatialHaystack(serializationInfo.ElementName);
+ return this;
+ }
+
+ /// <summary>
+ /// Sets the key name and additional field name to create a geospatial haystack index on.
+ /// </summary>
+ /// <typeparam name="TMember">The type of the member.</typeparam>
+ /// <typeparam name="TAdditionalMember">The type of the additional member.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="additionalMemberExpression">The additional member expression.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public IndexKeysBuilder<TDocument> GeoSpatialHaystack<TMember, TAdditionalMember>(Expression<Func<TDocument, TMember>> memberExpression, Expression<Func<TDocument, TAdditionalMember>> additionalMemberExpression)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var additionalSerializationInfo = _serializationInfoHelper.GetSerializationInfo(additionalMemberExpression);
+ _indexKeysBuilder = _indexKeysBuilder.GeoSpatialHaystack(serializationInfo.ElementName, additionalSerializationInfo.ElementName);
+ return this;
+ }
+
+ /// <summary>
+ /// Converts this object to a BsonDocument.
+ /// </summary>
+ /// <returns>
+ /// A BsonDocument.
+ /// </returns>
+ public override BsonDocument ToBsonDocument()
+ {
+ return _indexKeysBuilder.ToBsonDocument();
+ }
+
+ // protected methods
+ /// <summary>
+ /// Serializes the result of the builder to a BsonWriter.
+ /// </summary>
+ /// <param name="bsonWriter">The writer.</param>
+ /// <param name="nominalType">The nominal type.</param>
+ /// <param name="options">The serialization options.</param>
+ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
+ {
+ ((IBsonSerializable)_indexKeysBuilder).Serialize(bsonWriter, nominalType, options);
+ }
+
+ // private methods
+ private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions)
+ {
+ return memberExpressions
+ .Select(x => _serializationInfoHelper.GetSerializationInfo(x))
+ .Select(x => x.ElementName);
+ }
+ }
}
View
25 Driver/Builders/QueryBuilder.cs
@@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
@@ -38,6 +39,30 @@ public static IMongoQuery Null
get { return null; }
}
+ /// <summary>
+ /// Builds a query using a strongly-typed query builder.
+ /// </summary>
+ /// <typeparam name="TDocument"></typeparam>
+ /// <param name="queryBuilderFunction">A function that builds a query using the supplied query builder.</param>
+ /// <returns>an IMongoQuery.</returns>
+ public static IMongoQuery Build<TDocument>(Func<QueryBuilder<TDocument>, IMongoQuery> queryBuilderFunction)
+ {
+ var queryBuilder = new QueryBuilder<TDocument>();
+ return queryBuilderFunction(queryBuilder);
+ }
+
+ /// <summary>
+ /// Builds a query from an expression.
+ /// </summary>
+ /// <typeparam name="TDocument">The entity type.</typeparam>
+ /// <param name="expression">The query.</param>
+ /// <returns>An IMongoQuery.</returns>
+ public static IMongoQuery Where<TDocument>(Expression<Func<TDocument, bool>> expression)
+ {
+ var builder = new QueryBuilder<TDocument>();
+ return builder.Where(expression);
+ }
+
// public static methods
/// <summary>
/// Tests that the named array element contains all of the values (see $all).
View
116 Driver/Builders/SortByBuilder.cs
@@ -16,12 +16,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
-using MongoDB.Driver;
+using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
@@ -129,4 +130,117 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
}
}
+
+ /// <summary>
+ /// A builder for specifying a sort order.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public static class SortBy<TDocument>
+ {
+ /// <summary>
+ /// Adds keys to be sorted by in ascending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static SortByBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new SortByBuilder<TDocument>().Ascending(memberExpressions);
+ }
+
+ /// <summary>
+ /// Adds keys to be sorted by in descending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public static SortByBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ return new SortByBuilder<TDocument>().Descending(memberExpressions);
+ }
+ }
+
+ /// <summary>
+ /// A builder for specifying a sort order.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ [Serializable]
+ public class SortByBuilder<TDocument> : BuilderBase, IMongoSortBy
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private SortByBuilder _sortByBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SortByBuilder&lt;TDocument&gt;"/> class.
+ /// </summary>
+ public SortByBuilder()
+ {
+ _serializationInfoHelper = new BsonSerializationInfoHelper();
+ _sortByBuilder = new SortByBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Adds keys to be sorted by in ascending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions indicating which elements to sort by.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public SortByBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ var elementNames = GetElementNames(memberExpressions);
+ _sortByBuilder = _sortByBuilder.Ascending(elementNames.ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Adds keys to be sorted by in descending order.
+ /// </summary>
+ /// <param name="memberExpressions">The member expressions indicating which elements to sort by.</param>
+ /// <returns>
+ /// The builder (so method calls can be chained).
+ /// </returns>
+ public SortByBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
+ {
+ var elementNames = GetElementNames(memberExpressions);
+ _sortByBuilder = _sortByBuilder.Descending(elementNames.ToArray());
+ return this;
+ }
+
+ /// <summary>
+ /// Converts this object to a BsonDocument.
+ /// </summary>
+ /// <returns>
+ /// A BsonDocument.
+ /// </returns>
+ public override BsonDocument ToBsonDocument()
+ {
+ return _sortByBuilder.ToBsonDocument();
+ }
+
+ // protected methods
+ /// <summary>
+ /// Serializes the result of the builder to a BsonWriter.
+ /// </summary>
+ /// <param name="bsonWriter">The writer.</param>
+ /// <param name="nominalType">The nominal type.</param>
+ /// <param name="options">The serialization options.</param>
+ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
+ {
+ ((IBsonSerializable)_sortByBuilder).Serialize(bsonWriter, nominalType, options);
+ }
+
+ // private methods
+ private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions)
+ {
+ return memberExpressions
+ .Select(x => _serializationInfoHelper.GetSerializationInfo(x))
+ .Select(x => x.ElementName);
+ }
+ }
}
View
556 Driver/Builders/TypedQueryBuilder.cs
@@ -0,0 +1,556 @@
+/* Copyright 2010-2012 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+using System.Text.RegularExpressions;
+
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization;
+using MongoDB.Driver.Linq;
+using MongoDB.Driver.Linq.Utils;
+
+namespace MongoDB.Driver.Builders
+{
+ /// <summary>
+ /// Aids in building mongo queries based on type information.
+ /// </summary>
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
+ public class QueryBuilder<TDocument>
+ {
+ // private fields
+ private readonly BsonSerializationInfoHelper _serializationInfoHelper;
+ private readonly IBsonSerializer _rootSerializer;
+ private readonly PredicateTranslator _predicateTranslator;
+ private readonly UntypedQueryBuilder _queryBuilder;
+
+ // constructors
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueryBuilder&lt;TDocument&gt;"/> class.
+ /// </summary>
+ public QueryBuilder()
+ : this(new BsonSerializationInfoHelper(), null)
+ { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="QueryBuilder&lt;TDocument&gt;"/> class.
+ /// </summary>
+ /// <param name="serializationInfoHelper">The serialization info helper.</param>
+ /// <param name="rootSerializer">The root serializer.</param>
+ internal QueryBuilder(BsonSerializationInfoHelper serializationInfoHelper, IBsonSerializer rootSerializer)
+ {
+ _serializationInfoHelper = serializationInfoHelper;
+ _rootSerializer = rootSerializer;
+ _predicateTranslator = new PredicateTranslator(_serializationInfoHelper);
+ _queryBuilder = new UntypedQueryBuilder();
+ }
+
+ // public methods
+ /// <summary>
+ /// Tests that the named array element contains all of the values (see $all).
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="values">The values to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery All<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, IEnumerable<TValue> values)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var itemSerializationInfo = _serializationInfoHelper.GetItemSerializationInfo("All", serializationInfo);
+ var serializedValues = _serializationInfoHelper.SerializeValues(itemSerializationInfo, values);
+ return _queryBuilder.All(serializationInfo.ElementName, serializedValues);
+ }
+
+ /// <summary>
+ /// Tests that all the queries are true (see $and in newer versions of the server).
+ /// </summary>
+ /// <param name="queries">A list of subqueries.</param>
+ /// <returns>An IMongoQuery.</returns>
+ public IMongoQuery And(IEnumerable<IMongoQuery> queries)
+ {
+ return _queryBuilder.And(queries.ToArray());
+ }
+
+ /// <summary>
+ /// Tests that all the queries are true (see $and in newer versions of the server).
+ /// </summary>
+ /// <param name="queries">A list of subqueries.</param>
+ /// <returns>An IMongoQuery.</returns>
+ public IMongoQuery And(params IMongoQuery[] queries)
+ {
+ return And((IEnumerable<IMongoQuery>)queries);
+ }
+
+ /// <summary>
+ /// Tests that at least one item of the named array element matches a query (see $elemMatch).
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="elementQueryBuilderFunction">A function that builds a query using the supplied query builder.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery ElemMatch<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, Func<QueryBuilder<TValue>, IMongoQuery> elementQueryBuilderFunction)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var itemSerializationInfo = _serializationInfoHelper.GetItemSerializationInfo("ElemMatch", serializationInfo);
+ var elementQueryBuilder = new QueryBuilder<TValue>(_serializationInfoHelper, itemSerializationInfo.Serializer);
+ var elementQuery = elementQueryBuilderFunction(elementQueryBuilder);
+ return _queryBuilder.ElemMatch(serializationInfo.ElementName, elementQuery);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is equal to some value.
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="value">The value to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery EQ<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.EQ(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that an element of that name does or does not exist (see $exists).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Exists<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Exists(serializationInfo.ElementName);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is greater than some value (see $gt).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="value">The value to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery GT<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.GT(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is greater than or equal to some value (see $gte).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="value">The value to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery GTE<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.GTE(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is equal to one of a list of values (see $in).
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="values">The values to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery In<TValue>(Expression<Func<TDocument, TValue>> memberExpression, IEnumerable<TValue> values)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValues = _serializationInfoHelper.SerializeValues(serializationInfo, values);
+
+ return _queryBuilder.In(serializationInfo.ElementName, serializedValues);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is less than some value (see $lt).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="value">The value to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery LT<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.LT(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is less than or equal to some value (see $lte).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="value">The value to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery LTE<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.LTE(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element matches a regular expression (see $regex).
+ /// </summary>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="regex">The regex.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Matches(Expression<Func<TDocument, string>> memberExpression, BsonRegularExpression regex)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Matches(serializationInfo.ElementName, regex);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element matches a regular expression (see $regex).
+ /// </summary>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="pattern">The pattern.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Matches(Expression<Func<TDocument, string>> memberExpression, string pattern)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Matches(serializationInfo.ElementName, pattern);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element matches a regular expression (see $regex).
+ /// </summary>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="pattern">The pattern.</param>
+ /// <param name="options">The options.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Matches(Expression<Func<TDocument, string>> memberExpression, string pattern, string options)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Matches(serializationInfo.ElementName, pattern, options);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element matches a regular expression (see $regex).
+ /// </summary>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="regex">The regex.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Matches(Expression<Func<TDocument, string>> memberExpression, Regex regex)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Matches(serializationInfo.ElementName, regex);
+ }
+
+ /// <summary>
+ /// Tests that the modulus of the value of the named element matches some value (see $mod).
+ /// </summary>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="modulus">The modulus.</param>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Mod(Expression<Func<TDocument, int>> memberExpression, int modulus, int value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Mod(serializationInfo.ElementName, modulus, value);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is near some location (see $near).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="x">The x value of the origin.</param>
+ /// <param name="y">The y value of the origin.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Near<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double x, double y)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Near(serializationInfo.ElementName, x, y);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is near some location (see $near).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="x">The x value of the origin.</param>
+ /// <param name="y">The y value of the origin.</param>
+ /// <param name="maxDistance">The max distance.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Near<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double x, double y, double maxDistance)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Near(serializationInfo.ElementName, x, y, maxDistance);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is near some location (see $near).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="x">The x value of the origin.</param>
+ /// <param name="y">The y value of the origin.</param>
+ /// <param name="maxDistance">The max distance.</param>
+ /// <param name="spherical">if set to <c>true</c> [spherical].</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Near<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double x, double y, double maxDistance, bool spherical)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Near(serializationInfo.ElementName, x, y, maxDistance, spherical);
+ }
+
+ /// <summary>
+ /// Tests that the inverse of the query is true (see $not).
+ /// </summary>
+ /// <param name="query">The query.</param>
+ /// <returns></returns>
+ public IMongoQuery Not(IMongoQuery query)
+ {
+ return _queryBuilder.Not(query);
+ }
+
+ /// <summary>
+ /// Tests that an element does not equal the value (see $ne).
+ /// </summary>
+ /// <typeparam name="TMember"></typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery NE<TMember>(Expression<Func<TDocument, TMember>> memberExpression, TMember value)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, value);
+ return _queryBuilder.NE(serializationInfo.ElementName, serializedValue);
+ }
+
+ /// <summary>
+ /// Tests that an element of that name does not exist (see $exists).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery NotExists<TMember>(Expression<Func<TDocument, TMember>> memberExpression)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.NotExists(serializationInfo.ElementName);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is not equal to any item in a list of values (see $nin).
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression.</param>
+ /// <param name="values">The values to compare.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery NotIn<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, IEnumerable<TValue> values)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ var serializedValues = _serializationInfoHelper.SerializeValues(serializationInfo, values);
+ return _queryBuilder.NotIn(serializationInfo.ElementName, serializedValues);
+ }
+
+ /// <summary>
+ /// Tests that at least one of the subqueries is true (see $or).
+ /// </summary>
+ /// <param name="queries">The subqueries.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Or(IEnumerable<IMongoQuery> queries)
+ {
+ return _queryBuilder.Or(queries);
+ }
+
+ /// <summary>
+ /// Tests that at least one of the subqueries is true (see $or).
+ /// </summary>
+ /// <param name="queries">The subqueries.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Or(params IMongoQuery[] queries)
+ {
+ return Or((IEnumerable<IMongoQuery>)queries);
+ }
+
+ /// <summary>
+ /// Tests that the size of the named array is equal to some value (see $size).
+ /// </summary>
+ /// <typeparam name="TValue">The type of the enumerable member values.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="size">The size to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Size<TValue>(Expression<Func<TDocument, IEnumerable<TValue>>> memberExpression, int size)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Size(serializationInfo.ElementName, size);
+ }
+
+ /// <summary>
+ /// Tests that the type of the named element is equal to some type (see $type).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="type">The type to compare to.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Type<TMember>(Expression<Func<TDocument, TMember>> memberExpression, BsonType type)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.Type(serializationInfo.ElementName, type);
+ }
+
+ /// <summary>
+ /// Tests that a JavaScript expression is true (see $where).
+ /// </summary>
+ /// <param name="javascript">The javascript.</param>
+ /// <returns>
+ /// A query.
+ /// </returns>
+ public IMongoQuery Where(BsonJavaScript javascript)
+ {
+ return _queryBuilder.Where(javascript);
+ }
+
+ /// <summary>
+ /// Builds a query from an expression.
+ /// </summary>
+ /// <param name="expression">The expression.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery Where(Expression<Func<TDocument, bool>> expression)
+ {
+ var evaluatedExpression = PartialEvaluator.Evaluate(expression.Body);
+ return _predicateTranslator.BuildQuery(evaluatedExpression);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is within a circle (see $within and $center).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="centerX">The x coordinate of the origin.</param>
+ /// <param name="centerY">The y coordinate of the origin.</param>
+ /// <param name="radius">The radius of the circle.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery WithinCircle<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double centerX, double centerY, double radius)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.WithinCircle(serializationInfo.ElementName, centerX, centerY, radius);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is within a circle (see $within and $center).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="centerX">The x coordinate of the origin.</param>
+ /// <param name="centerY">The y coordinate of the origin.</param>
+ /// <param name="radius">The radius of the circle.</param>
+ /// <param name="spherical">if set to <c>true</c> [spherical].</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery WithinCircle<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double centerX, double centerY, double radius, bool spherical)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.WithinCircle(serializationInfo.ElementName, centerX, centerY, radius, spherical);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is within a polygon (see $within and $polygon).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="points">An array of points that defines the polygon (the second dimension must be of length 2).</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery WithinPolygon<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double[,] points)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.WithinPolygon(serializationInfo.ElementName, points);
+ }
+
+ /// <summary>
+ /// Tests that the value of the named element is within a rectangle (see $within and $box).
+ /// </summary>
+ /// <typeparam name="TMember">The member type.</typeparam>
+ /// <param name="memberExpression">The member expression representing the element to test.</param>
+ /// <param name="lowerLeftX">The x coordinate of the lower left corner.</param>
+ /// <param name="lowerLeftY">The y coordinate of the lower left corner.</param>
+ /// <param name="upperRightX">The x coordinate of the upper right corner.</param>
+ /// <param name="upperRightY">The y coordinate of the upper right corner.</param>
+ /// <returns>
+ /// An IMongoQuery.
+ /// </returns>
+ public IMongoQuery WithinRectangle<TMember>(Expression<Func<TDocument, TMember>> memberExpression, double lowerLeftX, double lowerLeftY, double upperRightX, double upperRightY)
+ {
+ var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
+ return _queryBuilder.WithinRectangle(serializationInfo.ElementName, lowerLeftX, lowerLeftY, upperRightX, upperRightY);
+ }
+ }
+}
View
835 Driver/Builders/UntypedQueryBuilder.cs
@@ -0,0 +1,835 @@
+/* Copyright 2010-2012 10gen Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+using MongoDB.Bson;
+
+namespace MongoDB.Driver.Builders
+{
+ internal class UntypedQueryBuilder
+ {
+ // public methods
+ /// <summary>
+ /// Tests that the named array element contains all of the values (see $all).
+ /// </summary>
+ /// <param name="name">The name of the element to test.</param>
+ /// <param name="values">The values to compare to.</param>
+ /// <returns>An IMongoQuery.</returns>
+ public IMongoQuery All(string name, IEnumerable<BsonValue> values)
+ {
+ if (name == null)
+ {
+ throw new ArgumentNullException("name");
+ }
+ if (values == null)
+ {
+ throw new ArgumentNullException("values");
+ }
+
+ var condition = new BsonDocument("$all", new BsonArray(values));
+ return new QueryDocument(name, condition);
+ }
+
+ /// <summary>
+ /// Tests that all the queries are true (see $and in newer versions of the server).
+ /// </summary>
+ /// <param name="queries">A list of subqueries.</param>
+ /// <returns>An IMongoQuery.</returns>
+ public IMongoQuery And(IEnumerable<IMongoQuery> queries)
+ {
+ if (queries == null)
+ {
+ throw new ArgumentNullException("queries");
+ }
+ if (!queries.Any())
+ {
+ throw new ArgumentOutOfRangeException("queries", "And cannot be called with zero queries.");
+ }
+
+ var queryDocument = new QueryDocument();
+ foreach (var query in queries)
+ {
+ if (query == null)
+ {
+ throw new ArgumentOutOfRangeException("queries", "One of the queries is null.");
+ }
+ foreach (var clause in query.ToBsonDocument())
+ {
+ AddAndClause(queryDocument, clause);
+ }
+ }
+
+ return queryDocument;
+ }
+
+ /// <summary>
+ /// Tests that all the queries are true (see $and in newer versions of the server).
+ /// </summary>