@ -28,14 +28,12 @@ namespace MongoDB.MongoDBClient {
private bool disposed = false ;
private MongoCollection collection ;
private BsonDocument query ;
private BsonDocument wrappedQuery = new BsonDocument ( ) ;
private BsonDocument fields ;
private BsonDocument orderBy ;
private bool snapshot ;
private QueryFlags flags ;
private int skip ;
private int limit ; // number of documents to return (enforced by cursor)
private int batchSize ; // number of documents to return in each reply
private bool explain ;
private bool frozen ; // TODO: freeze cursor once execution begins
#endregion
@ -70,6 +68,15 @@ namespace MongoDB.MongoDBClient {
#endregion
#region public methods
public MongoCursor < T > AddOption (
string name ,
object value
) {
if ( disposed ) { throw new ObjectDisposedException ( "MongoCursor" ) ; }
wrappedQuery [ name ] = value ;
return this ;
}
public MongoCursor < T > Batch (
int batchSize
) {
@ -102,7 +109,7 @@ namespace MongoDB.MongoDBClient {
// TODO: verbose argument?
public BsonDocument Explain ( ) {
explain = true ;
wrappedQuery [ "$ explain" ] = true ;
throw new NotImplementedException ( ) ;
}
@ -160,7 +167,8 @@ namespace MongoDB.MongoDBClient {
public MongoCursor < T > Hint (
BsonDocument hint
) {
throw new NotImplementedException ( ) ;
wrappedQuery [ "$hint" ] = hint ;
return this ;
}
public MongoCursor < T > Limit (
@ -171,6 +179,27 @@ namespace MongoDB.MongoDBClient {
return this ;
}
public MongoCursor < T > Max (
BsonDocument max
) {
wrappedQuery [ "$max" ] = max ;
return this ;
}
public MongoCursor < T > MaxScan (
int maxScan
) {
wrappedQuery [ "$maxscan" ] = maxScan ;
return this ;
}
public MongoCursor < T > Min (
BsonDocument min
) {
wrappedQuery [ "$min" ] = min ;
return this ;
}
public int Size ( ) {
if ( disposed ) { throw new ObjectDisposedException ( "MongoCursor" ) ; }
var command = new BsonDocument {
@ -197,7 +226,7 @@ namespace MongoDB.MongoDBClient {
public MongoCursor < T > Snapshot ( ) {
if ( disposed ) { throw new ObjectDisposedException ( "MongoCursor" ) ; }
this . snapshot = true ;
wrappedQuery [ "$snapshot" ] = true ;
return this ;
}
@ -205,7 +234,7 @@ namespace MongoDB.MongoDBClient {
BsonDocument orderBy
) {
if ( disposed ) { throw new ObjectDisposedException ( "MongoCursor" ) ; }
this . orderBy = orderBy ;
wrappedQuery [ "$orderby" ] = orderBy ;
return this ;
}
@ -221,10 +250,10 @@ namespace MongoDB.MongoDBClient {
int direction
) {
if ( disposed ) { throw new ObjectDisposedException ( "MongoCursor" ) ; }
orderBy = new BsonDocument {
var orderBy = new BsonDocument {
{ key , direction }
} ;
return this ;
return Sort ( orderBy ) ;
}
#endregion
@ -243,7 +272,7 @@ namespace MongoDB.MongoDBClient {
numberToReturn = limit ;
}
var message = new MongoQueryMessage ( collection , skip , numberToReturn , query , fields ) ;
var message = new MongoQueryMessage ( collection , flags , skip , numberToReturn , WrappedQuery ( ) , fields ) ;
connection . SendMessage ( message ) ;
var reply = connection . ReceiveMessage < T > ( ) ;
if ( ( reply . ResponseFlags & ResponseFlags . QueryFailure ) ! = 0 ) {
@ -268,6 +297,25 @@ namespace MongoDB.MongoDBClient {
}
return reply ;
}
private BsonDocument WrappedQuery ( ) {
if ( wrappedQuery . Elements . Count ( ) = = 0 ) {
return query ;
}
if ( query = = null ) { query = new BsonDocument ( ) ; }
if ( query . ContainsElement ( "$query" ) ) {
// it's already wrapped, just copy over the options
// note that options in wrappedQuery overwrite existing options in query
foreach ( var element in wrappedQuery ) {
query [ element . Name ] = element . Value ;
}
return query ;
} else {
wrappedQuery [ "$query" ] = query ;
return wrappedQuery ;
}
}
#endregion
}
}