Skip to content

Commit 9ed847f

Browse files
committed
Added support for sorting
1 parent e6d5bce commit 9ed847f

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

Query/MongoQuery.cs

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class MongoQuery : MongoQueryBase {
2525
/// </summary>
2626
public MongoQuery(MongoCollection collection)
2727
: base (collection) {
28-
this._Parameters = new BsonDocument();
28+
_Parameters = new BsonDocument();
29+
_sortParameters = new BsonDocument();
2930
}
3031

3132
/// <summary>
@@ -41,6 +42,7 @@ public MongoQuery(MongoDatabase database, string collection)
4142

4243
//the class that actually queries the database
4344
private readonly BsonDocument _Parameters;
45+
private readonly BsonDocument _sortParameters;
4446

4547
/// <summary>
4648
/// Gets the query document. This document can be used for filtering.
@@ -51,7 +53,14 @@ public BsonDocument QueryDocument
5153
get
5254
{
5355
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+
5564
return doc;
5665
}
5766
}
@@ -360,35 +369,35 @@ public IEnumerable<MongoDocument> Select() {
360369
/// Selects the records from the database that matches this query
361370
/// </summary>
362371
public IEnumerable<MongoDocument> Select(int skip, int take) {
363-
return this.Select(skip, take, QueryOptionTypes.None);
372+
return Select(skip, take, QueryOptionTypes.None);
364373
}
365374

366375
/// <summary>
367376
/// Selects the records from the database that matches this query
368377
/// </summary>
369378
public IEnumerable<MongoDocument> Select(QueryOptionTypes options) {
370-
return this.Select(Mongo.DefaultSkipCount, Mongo.DefaultTakeCount, options);
379+
return Select(Mongo.DefaultSkipCount, Mongo.DefaultTakeCount, options);
371380
}
372381

373382
/// <summary>
374383
/// Selects the records from the database that matches this query
375384
/// </summary>
376385
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);
378387
}
379388

380389
/// <summary>
381390
/// Selects the records from the database that matches this query
382391
/// </summary>
383392
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);
385394
}
386395

387396
/// <summary>
388397
/// Selects the records from the database that matches this query
389398
/// </summary>
390399
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);
392401
}
393402

394403
/// <summary>
@@ -397,20 +406,20 @@ public IEnumerable<MongoDocument> Select(QueryOptionTypes options, params string
397406
public IEnumerable<MongoDocument> Select(int skip, int take, QueryOptionTypes options, params string[] fields) {
398407
if (fields==null) fields = new string[]{};
399408
//create the request to use
400-
QueryRequest request = new QueryRequest(this.Collection);
409+
var request = new QueryRequest(Collection);
401410
request.Fields.AddRange(fields); //gives an error when fields is null
402411
request.Skip = skip;
403412
request.Take = take;
404413
request.Options = options;
405-
request.Parameters = this._Parameters;
414+
request.Parameters = _Parameters;
406415

407416
//send the request and get the response
408-
QueryResponse response = this.Collection.Database.Connection
417+
var response = this.Collection.Database.Connection
409418
.SendRequest(request) as QueryResponse;
410419

411420
//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);
414423

415424
//and return the records
416425
IEnumerable<MongoDocument> documents = response.Documents.AsEnumerable();
@@ -714,9 +723,9 @@ public void Set(BsonDocument document) {
714723
public void Unset(params string[] fields) {
715724

716725
//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);
720729
}
721730

722731
//send the command
@@ -730,13 +739,13 @@ public void Unset(params string[] fields) {
730739
public void Increment(params string[] fields) {
731740

732741
//create the document
733-
BsonDocument document = new BsonDocument{UseRawFieldNames = true};
742+
var document = new BsonDocument{UseRawFieldNames = true};
734743
foreach (var field in fields) {
735-
document.Set<int>(field, 1);
744+
document.Set(field, 1);
736745
}
737746

738747
//send the command
739-
this.Increment(document);
748+
Increment(document);
740749

741750
}
742751

@@ -757,7 +766,7 @@ public void Increment(BsonDocument document) {
757766
//recast each to an integer value - I'm not sure
758767
//if any numeric type can be used in this instance
759768
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));
761770
}
762771

763772
//send the update request
@@ -769,20 +778,22 @@ public void Increment(BsonDocument document) {
769778
private void _SendUpdate(string type, UpdateOptionTypes options, BsonDocument changes) {
770779

771780
//update the changes to actually make
772-
BsonDocument document = new BsonDocument();
781+
var document = new BsonDocument();
773782
document[type] = changes;
774783

775784
//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+
};
780791

781792
//make sure something is found to change
782793
if (request.Modifications.FieldCount == 0) { return; }
783794

784795
//send the request and get the response
785-
this.Collection.Database.Connection.SendRequest(request);
796+
Collection.Database.Connection.SendRequest(request);
786797

787798
}
788799

@@ -836,6 +847,17 @@ public MongoQuery SetFilter(BsonObject doc)
836847
}
837848
#endregion
838849

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+
}
839861
}
840862

841863
}

0 commit comments

Comments
 (0)