@@ -25,7 +25,8 @@ public class MongoQuery : MongoQueryBase {
25
25
/// </summary>
26
26
public MongoQuery ( MongoCollection collection )
27
27
: base ( collection ) {
28
- this . _Parameters = new BsonDocument ( ) ;
28
+ _Parameters = new BsonDocument ( ) ;
29
+ _sortParameters = new BsonDocument ( ) ;
29
30
}
30
31
31
32
/// <summary>
@@ -41,6 +42,7 @@ public MongoQuery(MongoDatabase database, string collection)
41
42
42
43
//the class that actually queries the database
43
44
private readonly BsonDocument _Parameters ;
45
+ private readonly BsonDocument _sortParameters ;
44
46
45
47
/// <summary>
46
48
/// Gets the query document. This document can be used for filtering.
@@ -51,7 +53,14 @@ public BsonDocument QueryDocument
51
53
get
52
54
{
53
55
var doc = new BsonDocument ( ) ;
54
- doc . Merge ( _Parameters ) ;
56
+ if ( _sortParameters . FieldCount == 0 )
57
+ doc . Merge ( _Parameters ) ;
58
+ else
59
+ {
60
+ doc [ "query" ] = _Parameters ;
61
+ doc [ "orderby" ] = _sortParameters ;
62
+ }
63
+
55
64
return doc ;
56
65
}
57
66
}
@@ -360,35 +369,35 @@ public IEnumerable<MongoDocument> Select() {
360
369
/// Selects the records from the database that matches this query
361
370
/// </summary>
362
371
public IEnumerable < MongoDocument > Select ( int skip , int take ) {
363
- return this . Select ( skip , take , QueryOptionTypes . None ) ;
372
+ return Select ( skip , take , QueryOptionTypes . None ) ;
364
373
}
365
374
366
375
/// <summary>
367
376
/// Selects the records from the database that matches this query
368
377
/// </summary>
369
378
public IEnumerable < MongoDocument > Select ( QueryOptionTypes options ) {
370
- return this . Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , options ) ;
379
+ return Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , options ) ;
371
380
}
372
381
373
382
/// <summary>
374
383
/// Selects the records from the database that matches this query
375
384
/// </summary>
376
385
public IEnumerable < MongoDocument > Select ( params string [ ] fields ) {
377
- return this . Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , QueryOptionTypes . None , fields ) ;
386
+ return Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , QueryOptionTypes . None , fields ) ;
378
387
}
379
388
380
389
/// <summary>
381
390
/// Selects the records from the database that matches this query
382
391
/// </summary>
383
392
public IEnumerable < MongoDocument > Select ( int skip , int take , params string [ ] fields ) {
384
- return this . Select ( skip , take , QueryOptionTypes . None , fields ) ;
393
+ return Select ( skip , take , QueryOptionTypes . None , fields ) ;
385
394
}
386
395
387
396
/// <summary>
388
397
/// Selects the records from the database that matches this query
389
398
/// </summary>
390
399
public IEnumerable < MongoDocument > Select ( QueryOptionTypes options , params string [ ] fields ) {
391
- return this . Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , options , fields ) ;
400
+ return Select ( Mongo . DefaultSkipCount , Mongo . DefaultTakeCount , options , fields ) ;
392
401
}
393
402
394
403
/// <summary>
@@ -397,20 +406,20 @@ public IEnumerable<MongoDocument> Select(QueryOptionTypes options, params string
397
406
public IEnumerable < MongoDocument > Select ( int skip , int take , QueryOptionTypes options , params string [ ] fields ) {
398
407
if ( fields == null ) fields = new string [ ] { } ;
399
408
//create the request to use
400
- QueryRequest request = new QueryRequest ( this . Collection ) ;
409
+ var request = new QueryRequest ( Collection ) ;
401
410
request . Fields . AddRange ( fields ) ; //gives an error when fields is null
402
411
request . Skip = skip ;
403
412
request . Take = take ;
404
413
request . Options = options ;
405
- request . Parameters = this . _Parameters ;
414
+ request . Parameters = _Parameters ;
406
415
407
416
//send the request and get the response
408
- QueryResponse response = this . Collection . Database . Connection
417
+ var response = this . Collection . Database . Connection
409
418
. SendRequest ( request ) as QueryResponse ;
410
419
411
420
//save this cursor for later
412
- MongoCursor cursor = new MongoCursor ( request , response . CursorId , response . TotalReturned ) ;
413
- this . Collection . Database . RegisterCursor ( cursor ) ;
421
+ var cursor = new MongoCursor ( request , response . CursorId , response . TotalReturned ) ;
422
+ Collection . Database . RegisterCursor ( cursor ) ;
414
423
415
424
//and return the records
416
425
IEnumerable < MongoDocument > documents = response . Documents . AsEnumerable ( ) ;
@@ -714,9 +723,9 @@ public void Set(BsonDocument document) {
714
723
public void Unset ( params string [ ] fields ) {
715
724
716
725
//mark the fields to be removed
717
- BsonDocument remove = new BsonDocument ( ) ;
718
- foreach ( string field in fields ) {
719
- remove . Set < int > ( field , 1 ) ;
726
+ var remove = new BsonDocument ( ) ;
727
+ foreach ( var field in fields ) {
728
+ remove . Set ( field , 1 ) ;
720
729
}
721
730
722
731
//send the command
@@ -730,13 +739,13 @@ public void Unset(params string[] fields) {
730
739
public void Increment ( params string [ ] fields ) {
731
740
732
741
//create the document
733
- BsonDocument document = new BsonDocument { UseRawFieldNames = true } ;
742
+ var document = new BsonDocument { UseRawFieldNames = true } ;
734
743
foreach ( var field in fields ) {
735
- document . Set < int > ( field , 1 ) ;
744
+ document . Set ( field , 1 ) ;
736
745
}
737
746
738
747
//send the command
739
- this . Increment ( document ) ;
748
+ Increment ( document ) ;
740
749
741
750
}
742
751
@@ -757,7 +766,7 @@ public void Increment(BsonDocument document) {
757
766
//recast each to an integer value - I'm not sure
758
767
//if any numeric type can be used in this instance
759
768
foreach ( var item in document . GetValues ( ) ) {
760
- document . Set < int > ( item . Key , document . Get < int > ( item . Key , 1 ) ) ;
769
+ document . Set ( item . Key , document . Get ( item . Key , 1 ) ) ;
761
770
}
762
771
763
772
//send the update request
@@ -769,20 +778,22 @@ public void Increment(BsonDocument document) {
769
778
private void _SendUpdate ( string type , UpdateOptionTypes options , BsonDocument changes ) {
770
779
771
780
//update the changes to actually make
772
- BsonDocument document = new BsonDocument ( ) ;
781
+ var document = new BsonDocument ( ) ;
773
782
document [ type ] = changes ;
774
783
775
784
//create the request to use
776
- UpdateRequest request = new UpdateRequest ( this . Collection ) ;
777
- request . Modifications = document ;
778
- request . Parameters = this . _Parameters ;
779
- request . Options = options ;
785
+ var request = new UpdateRequest ( Collection )
786
+ {
787
+ Modifications = document ,
788
+ Parameters = _Parameters ,
789
+ Options = options
790
+ } ;
780
791
781
792
//make sure something is found to change
782
793
if ( request . Modifications . FieldCount == 0 ) { return ; }
783
794
784
795
//send the request and get the response
785
- this . Collection . Database . Connection . SendRequest ( request ) ;
796
+ Collection . Database . Connection . SendRequest ( request ) ;
786
797
787
798
}
788
799
@@ -836,6 +847,17 @@ public MongoQuery SetFilter(BsonObject doc)
836
847
}
837
848
#endregion
838
849
850
+ /// <summary>
851
+ /// Sorts the query results by the field prrovided.
852
+ /// </summary>
853
+ /// <param name="field">The field to sort by.</param>
854
+ /// <param name="ascending">if set to <c>true</c> sorting will be in [ascending] order.</param>
855
+ /// <returns></returns>
856
+ public MongoQuery SortBy ( string field , bool ascending = true )
857
+ {
858
+ _sortParameters . Set ( field , ascending ? 1 : - 1 ) ;
859
+ return this ;
860
+ }
839
861
}
840
862
841
863
}
0 commit comments