Search code examples
mongodbdelphiunidac

How to retrieve two or more fields by using TUniQuery in Delphi/MongoDB?


How to retrieve two or more fields by using TUniQuery (from UniDAC Library) in Delphi accessing MongoDB?

Example: when using MongoSH, it's possible to do equivalent retrieve like this:

db.myCollection.find({}, {Id: 1, Number: 1})

Result is:

{
  _id: ObjectId("6463d53f8e2260611bed7216"),
  Id: 575682,
  Number: '99621800'
},
{
  _id: ObjectId("6463d53f8e2260611bed7217"),
  Id: 578559,
  Number: '364543222'
},
.
.
.

In Delphi, it works like this:

UniQuery1.Close;
UniQuery1.SQL.Clear;
UniQuery1.SQL.Text := '{"find":"myCollection", "filter":{Id: 530142}}';
UniQuery1.Open;

In the example above I can't include fields "Id" and "Number".

How to do the same retrieve by using TUniQuery (UniDAC) in Delphi with MongoDB?


Solution

  • It works like @dododo:

    UniQuery1.Close;
    UniQuery1.SQL.Clear;
    UniQuery1.SQL.Text := '{"find":"myCollection", "filter":{"Id": 535662}, "projection": {Id: 1, Number: 1}}';
    UniQuery1.Open;
    

    Thanks @dododo!