You are not logged in.
Pages: 1
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
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
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
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
Thanks
Offline
Pages: 1