#1 2014-05-21 10:02:43

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

[mongodb] FindONe and FindDoc

hello, I'm trying mongodb library.

In the blog there was the example of searching for a document:
  doc: = Coll.FindDoc ('{_id:?}', [5]);
  doc: = Coll.FindOne (5); / / Same as previous

but doing a test, I noticed that the result is rather different:

FindOne: {"_id": "idtest", "Name", "test", "Number": 1000}
Finddoc: [{"_id": "idtest", "Name", "test", "Number": 1000}]

Finddoc returns an array.

is wanted or is it a bug?

Offline

#2 2014-05-21 12:14:32

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [mongodb] FindONe and FindDoc

pre-solution:

I found the problem:

     finddoc function (Criteria: PUTF8Char; const Params: array of const;
       NumberToReturn: integer = maxInt; NumberToSkip: Integer = 0;
       Flags: TMongoQueryFlags = []): variant; overloaded;

maxint for NumberToReturn is wrong.

I solved this way:

doc -> docs
1 <-> maxint

     finddoc function (Criteria: PUTF8Char; const Params: array of const; NumberToReturn: integer = 1; NumberToSkip: Integer = 0;
       Flags: TMongoQueryFlags = []): variant; overloaded;

     FindDocs function (Criteria: PUTF8Char; const Params: array of const;
       NumberToReturn: integer = maxInt; NumberToSkip: Integer = 0;
       Flags: TMongoQueryFlags = []): variant; overloaded;


old finddoc must be convertitit in findDocs.

Offline

#3 2014-05-21 13:41:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: [mongodb] FindONe and FindDoc

This is a feature, clearly stated in the doc of the methods.
Perhaps the high-level introduction text is confusing.

FindOne() returns one JSON object, whereas FindDoc() may return several objects, so by design it returns a JSON array.

Offline

#4 2014-05-22 07:15:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: [mongodb] FindONe and FindDoc

You should better write:

var
  Doc: variant;
  DOCA: PDocVariantData;

  doc: = Coll.FindDoc ('{Name:?}', ['test12']);
  DOCA: = DocVariantData (doc);
  if DOCA<>nil then
  for i: = 0 to docA.Count-1 do
  begin
    Memo1.Lines.add ('Name:' + DOCA.Values[i]. Name);
    Memo1.Lines.add ('Number:' + DOCA.Values[i]. Number);
   end;

or, via late-binding only:

var
  Doc: variant;

  doc: = Coll.FindDoc ('{Name:?}', ['test12']);
  for i: = 0 to doc._Count-1 do
  begin
    Memo1.Lines.add ('Name:' + DOCA._(i).Name);
    Memo1.Lines.add ('Number:' + DOCA._(i).Number);
   end;

or the one I prefer:

var
  Docs: TVariantDynArray;

  Coll.FindDocs ('{Name:?}', ['test12'],Docs);
  for i: = 0 to high(Docs) do
  begin
    Memo1.Lines.add ('Name:' + Docs[i].Name);
    Memo1.Lines.add ('Number:' + Docs[i].Number);
   end;

Offline

#5 2014-05-22 07:20:07

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [mongodb] FindONe and FindDoc

Thanks

Offline

Board footer

Powered by FluxBB