stijnsanders / TMongoWire

Delphi MongoDB driver
MIT License
102 stars 37 forks source link

Displaying values from embedded document #32

Closed scaleman114 closed 7 years ago

scaleman114 commented 7 years ago

If I have a e.g a collection of 'orders' (orderno, date, ordertotal etc.) I know I can access an element of that order by using it's key name. e.g 'orderno'. But if I have embedded within the orders 'items' (itemid, qty, description etc.), how do I display these values? I have tried - (with d as my returned IBSONDocument) Memo.Lines.Add(VarToStr(d['items.description'])); but I get an empty string.

stijnsanders commented 7 years ago

I suspect items will actually contain an array of objects (with itemid, qty, etc.), then d['items'] will result in an OleVariant of type variant array of objects (IBSONDocument instances in this case). So either use BSON(d['items'][0])['description'] or declare a local variable, e.g. var v:OleVariant; then v:=d['items']; and use

for i:=VarArrayLowBound(v,1) to VarArrayHighBound(v,1) do
begin 
  d1:=BSON(v[i]);
  d1['description'];//...
end;

If you're processing large sets of documents in an array, there's also BSONEnum but I may need to write a specific example project to show how that works.

scaleman114 commented 7 years ago

I was close just needed that little explanation. Many thanks.