#1 2014-10-02 06:36:33

bigheart
Member
Registered: 2014-08-01
Posts: 45

[mongoDB] How to use for dvArray kind of TDocVariant

Hi!

I got some problem when using mORMot for TMongoCollection.AggregateDoc

  var
      LDoc: variant;
   
  LDoc:= Coll.AggregateDoc('{$sort:{_id:-1}},{$limit:1}', []); ===> single value is returned, OK!

but

  LDoc:= Coll.AggregateDoc('{$sort:{_id:-1}},{$limit:5}', []);  ===> multiple values are returned(5 values)

In this case, how to access the second value like: Lvar[1].value;

According to the manual:

  if LDoc._kind = ord(dvArray) then ===> error occured here that "Invalid variant type 0 invoke"
  begin
    showmessage(LDoc._(1));
  end;

Help me please!

Offline

#2 2014-10-02 08:41:51

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

Re: [mongoDB] How to use for dvArray kind of TDocVariant

I suspect your returned LDoc is NOT a TDocVariant, but null.

Could you debug and step in TMongoConnection.RunCommand() to see what is returned by the server?

Offline

#3 2014-10-02 09:13:58

bigheart
Member
Registered: 2014-08-01
Posts: 45

Re: [mongoDB] How to use for dvArray kind of TDocVariant

I have executed as below:

var
  AQry: string;
  LDoc: variant;

  AQry := '{ aggregate: "COLLECTNAME",   pipeline: [{$sort:{_id:-1}},{$limit:5}], allowDiskUse: true}';
  FDB.RunCommand(BSONVariant(AQry), LDoc); 

with debugging LDoc, LDoc has a data, not null;

but

  showmessage(Utf8ToString(VariantToUtf8(LDoc.Value))); ===> "null" displayed

Last edited by bigheart (2014-10-02 09:15:10)

Offline

#4 2014-10-03 07:37:29

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

Re: [mongoDB] How to use for dvArray kind of TDocVariant

What is LDoc content?

Offline

#5 2014-10-10 08:55:25

bigheart
Member
Registered: 2014-08-01
Posts: 45

Re: [mongoDB] How to use for dvArray kind of TDocVariant

LDoc contents are datetime and "a":"1" format data.

When debugging with "class procedure TDocVariant.GetSingleOrDefault" function in SynCommons.pas is excuted,

recursive call itself one time.

Trace into that function again,

TDocVariant(DocVariantArray).count is 5,

also TDocVariantData(DocVariantArray).Values[0] ~ TDocVariantData(DocVariantArray).Values[4] has exact data what i want.

But condition as "TDocVariant(DocVariantArray).count  <> 1" in GetSingleOrDefault(...) function,

result := default;

is excuted;

And next "result" is not assigned.

How can i have multiple data from AggregateDoc(...).

Offline

#6 2019-10-03 10:15:33

sodri126
Member
Registered: 2019-10-01
Posts: 4

Re: [mongoDB] How to use for dvArray kind of TDocVariant

Yes, I got error when the data is more than 2. How to solve it when I got more than one in AggregateDoc()?

Offline

#7 2019-10-03 19:48:27

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

Re: [mongoDB] How to use for dvArray kind of TDocVariant

TDocVariant(DocVariantArray) is not correct.
You need to transtype as TDocVariantVariant(DocVariantArray).

Offline

#8 2019-10-06 16:52:03

sodri126
Member
Registered: 2019-10-01
Posts: 4

Re: [mongoDB] How to use for dvArray kind of TDocVariant

is any TDocVariantVariant in mormot? because i have already typed it and Undefined the class

Offline

#9 2019-10-07 07:41:18

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 210

Re: [mongoDB] How to use for dvArray kind of TDocVariant

sodri126 wrote:

is any TDocVariantVariant in mormot? because i have already typed it and Undefined the class

It was a typo error, he meant TDocVariantData

Offline

Board footer

Powered by FluxBB