2

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

Marco Andreolli
  • 316
  • 5
  • 19

2 Answers2

1

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;
Marco Andreolli
  • 316
  • 5
  • 19
  • Well, maybe we misunderstood each other. What you've shown in your answer will be built if you replace on [line 725](https://github.com/gerald-lindsly/mongo-delphi-driver/blob/master/MongoDB.pas#L725) word `$sort` with `$orderby`. Because either you pass a query to the cursor constructor, or specify `TMongoCursor.sort` member in which case the same as in your answer is constructed (except with the wrong option `$sort`). I'm trying to say that it's a bug in that library. – Victoria Jun 12 '17 at 10:48
  • Could you verify it and suggest a fix to your filed issue if that helps, please? Using code from your question with library modified on that line 725.. – Victoria Jun 13 '17 at 06:39
  • I had tried to replace it, but it didn't work mhh, probably I didn't compiled. Now I do another test.... Yes I do – Marco Andreolli Jun 13 '17 at 06:47
  • Because if you look closer, on line 722 is a test whether the `sort` member is assigned and if so, exactly the same query as in this answer is built, just with a wrong option `$sort`. – Victoria Jun 13 '17 at 06:55
  • ok I understand, I replace `$sort` with `$orderby` in line 722 I rebuild the library but don't work, I know is the same code It's strange probably I miss something – Marco Andreolli Jun 13 '17 at 07:02
  • Ok, now I underestand my mistake. First key must be a string (pass with quotes....), Second after build library you must do "Install"!!!. Sorry If wou waste time. Now I contact the maintainer to fix the bug. Thanks @Victoria for all! – Marco Andreolli Jun 13 '17 at 07:21
  • 1
    You're welcome! Maybe you might add a note that there's an issue in the library for future visitors with the same problem. – Victoria Jun 13 '17 at 07:34
1

I modified the github repo with Marco's fix, but this is untested. The library is not currently maintained and I no longer have a license to the compiler.