Search code examples
mongodbdelphimongo-c-driver

Sorting seems doesn't work mongo-delphi-driver


This simple snippet doesn't work, I would like sort the document by name but the documents returned have no order

procedure TForm1.Button1Click(Sender: TObject);
var
  cursor : TMongoCursor;
begin

  cursor := TMongoCursor.Create(BSON([]));
  cursor.sort := BSON(['name','1']);
  if mongo.find(ns, cursor) then begin
    while cursor.next() do begin
      ShowMessage(cursor.value().find('name').value);
    end;
  end;

end;

[EDIT]: solved with latest fix on Jun 15, 2017


Solution

  • Ok, with sort property the ordering doesn't work, but with $orderby operator I can do the same thing.

    If it can be useful, this is a simple snippet

    procedure TForm1.Button1Click(Sender: TObject);
    var
        cursor : TMongoCursor;
        bb : TBsonBuffer;
        query, b : TBson;
    begin
    
      bb := TBsonBuffer.Create();
      bb.append('$query', BSON([]));
      bb.append('$orderby', BSON(['name',-1]));
      query := bb.finish();
      cursor := TMongoCursor.Create(query);
      if mongo.find(ns, cursor) then begin
      while cursor.next() do begin
        ShowMessage(cursor.value().find('name').value);
      end;    
    
    end;