#1 2016-10-15 15:04:57

KeithG
Member
From: France
Registered: 2016-10-15
Posts: 11

Changing values in a TDocVariant.

Hi,

I'm trying to write updated values back to the json record for posting back to the mormot database,  but none of my changes stick - ie it reverts back to the original values.

Here's my code:  Content[lRow]  is a variant of type TDocVariant.

      lIndex := TDocVariantData(lDisplayObject.Content[lRow]).GetValueIndex(RawUTF8(lTagName));

      // Tried this first

      DocVariantData(lDisplayObject.Content[lRow]).Values[lIndex] := TFixedConv.Str2Double(NewTextValue);

      // Then tried this when the above doesn't work

      vd := DocVariantData(lDisplayObject.Content[lRow])^;
      vd.Values[lIndex] := TFixedConv.Str2Double(NewTextValue);
      lDisplayObject.Content[lRow] := _Json(vd.ToJSon, [dvoJSONParseDoNotTryCustomVariants]);

      // And this doesn't work either.
      lDisplayObject.Content[lRow].Modified := GetServerISOTime;

The name of the field to be modified is in TagName.  and lIndex is correct.

At the end of this routine I expect the field to be updated and the Modified date to be correct,  but neither is true. 

Can someone tell me what I'm doing wrong?

Regards
Keith

Offline

#2 2016-10-15 18:40:35

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

Re: Changing values in a TDocVariant.

How is your COntent[lRow] value created?

Offline

#3 2016-10-15 21:06:55

KeithG
Member
From: France
Registered: 2016-10-15
Posts: 11

Re: Changing values in a TDocVariant.

lDoc is a RawUTF8 string result of a query on a MongoDB - an array of documents.  Then:

    lSO := _JSon(RawUTF8(lDoc), [dvoJSONParseDoNotTryCustomVariants]);

    for lRecord := 0 to TDocVariantData(lSO).Count - 1 do
    begin
        v := TDocVariantData(lSO).Value[lRecord];
       // seems to be necessary or weird things happen!
       _Unique(v);

       Content[lRow] := v;
    end;

BTW I'm not sure if all this is correct as I'm very new to Mormot and MongoDB,  but it seems to work ok until I try to replace a value with a new one.  The idea is to store the document in an array so that it can be displayed in virtual treeview calls,  and edited and the values update when necesssary.

Regards
Keith

Offline

#4 2016-10-17 15:24:11

KeithG
Member
From: France
Registered: 2016-10-15
Posts: 11

Re: Changing values in a TDocVariant.

I seem to have fixed this by switching _Unique to _UniqueFast,  however I still have a problem writing dates.  The dates are being put in as strings instead of $date values.  How can I force this to work?

Regards
Keith

Offline

#5 2016-10-17 15:29:25

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

Re: Changing values in a TDocVariant.

Search the forum for this.
And you will find http://synopse.info/forum/viewtopic.php?id=3545

Offline

#6 2016-10-17 17:09:51

KeithG
Member
From: France
Registered: 2016-10-15
Posts: 11

Re: Changing values in a TDocVariant.

Interestingly,  that didn't work for us,  however this did:

  s := GetServerISOTime;
  TDocVariant.New(lDate);
  TDocVariantData(lDate).Value['$date'] := s;
  lDisplayObject.Content[lRow].Modified := lDate;

After that our JSON string is correct and everything gets correctly written to our database.  Anything else just causes a string value to be written.

Regards
Keith.

Offline

Board footer

Powered by FluxBB