Skip to content

Commit 59ec66b

Browse files
author
rstam
committed
Added a way for client code to see what MongoDB query would be sent to a server when a LINQ query is executed.
1 parent 1a83967 commit 59ec66b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Driver/Linq/MongoQueryProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public MongoCollection Collection
5858
}
5959

6060
// public methods
61+
/// <summary>
62+
/// Builds the MongoDB query that will be sent to the server when the LINQ query is executed.
63+
/// </summary>
64+
/// <returns>The MongoDB query.</returns>
65+
public IMongoQuery BuildMongoQuery<T>(MongoQueryable<T> query)
66+
{
67+
var translatedQuery = MongoQueryTranslator.Translate(this, ((IQueryable)query).Expression);
68+
return ((SelectQuery)translatedQuery).BuildQuery();
69+
}
70+
6171
/// <summary>
6272
/// Creates a new instance of MongoQueryable{{T}} for this provider.
6373
/// </summary>

Driver/Linq/MongoQueryable.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public IEnumerator<T> GetEnumerator()
8282
return ((IEnumerable<T>)_provider.Execute(_expression)).GetEnumerator();
8383
}
8484

85+
/// <summary>
86+
/// Gets the MongoDB query that will be sent to the server when this LINQ query is executed.
87+
/// </summary>
88+
/// <returns>The MongoDB query.</returns>
89+
public IMongoQuery GetMongoQuery()
90+
{
91+
return _provider.BuildMongoQuery(this);
92+
}
93+
8594
// explicit implementation of IEnumerable
8695
IEnumerator IEnumerable.GetEnumerator()
8796
{

0 commit comments

Comments
 (0)