stijnsanders / TMongoWire

Delphi MongoDB driver
MIT License
102 stars 37 forks source link

TMongoWire sorting a collection #31

Closed scaleman114 closed 7 years ago

scaleman114 commented 7 years ago

I am new to mongodb and I am trying out TMongoWire with Delphi. Does anyone know how to sort a collection by field using TMongoWire? Such as by 'name'? Also how would I find all surnames beginning with say 'Da'?

scaleman114 commented 7 years ago

Ok worked out how to use regex but cannot see how to use the (i) ignore case option.

stijnsanders commented 7 years ago

According to this it should be something like

MyMongoWireQuery.Query('test',BSON(['$query',BSON,'$orderby',BSON(['name',-1])]));
stijnsanders commented 7 years ago

If you use bsonRegExPrefix you should be able to add "i" after the final "\", If you use CoRegExp.Create (and the VBScript_RegExp_55_TLB unit, and {$DEFINE BSON_SUPPORT_REGEX}), IRegExp has an IgnoreCase boolean property. (see also)

scaleman114 commented 7 years ago

Ok thanks. Can get the query: MyMongoWireQuery.Query('test',BSON(['$query',BSON,'$orderby',BSON(['name',-1])])); This works fine. But would it be possible to provide an example for the 'ignore case' switch, because when you prefix bsonRegExPrefix with the expression, no "/" is required.

stijnsanders commented 7 years ago

Could you show me some code of what you've got now using regexes?

scaleman114 commented 7 years ago
`procedure TMainForm.LoadItemQuery(qName:String;qValue:String);

var
  q:TMongoWireQuery;
  d:IBSONDocument;
begin
  ListView1.Items.BeginUpdate;
  try
    ListView1.Items.Clear;
    d:=BSON([qName,bsonRegExPrefix + qValue]);
    q:=TMongoWireQuery.Create(FMongoWire);
    try
      q.Query(mwx1Collection,d);
     while q.Next(d) do LoadItem(ListView1.Items.Add,d);
    finally
      q.Free;
    end;
    UpdateCount;
  finally
    ListView1.Items.EndUpdate;
  end;
end;`

The above is taken from your example and altered slightly. If I enter say 'J' as the qValue I can get all names beginning with 'J'. But id I enter '/j/i' as qValue no lines are returned.

stijnsanders commented 7 years ago

I've had a quick look. You may need to use the $regex operator.