TMongoWire icon indicating copy to clipboard operation
TMongoWire copied to clipboard

add object to array

Open DDAGitHub opened this issue 8 years ago • 4 comments

Hello. Help, please, I can't to find the solution.

There is the object { "_id" : "id1", "data" : [ { "jobs" : [ { "title" : "t1", "type" : "tt1", "category" : "c1" }, { "title" : "t2", "type" : "tt2", "category" : "c2" } ] } ] } I want to add the third job to the jobs array. doc:=FMongoWire.Get(mwCollectionPersons,dSelector); d0:= JSON(doc['data'][0]); jobs_ru:= d0['jobs']; n:= VarArrayHighBound(jobs_ru,1)+1; VarArrayRedim(jobs_ru, n+1); jobs_ru[n]:=JSON(['title','t3','type','tt3']);

fMongoWire.Update(mwCollectionPersons,dSelector,doc); I have found that this code does not increase length of jobs array:

Could you posible to help me? With best regards, Dmitry.

DDAGitHub avatar May 24 '17 10:05 DDAGitHub

I think you probably should look into the $push operator instead of posting a full update of the document.

fMongoWire.Update(mxCollectionPersons,dSelector,JSON(['$push{','data[0].jobs{','title','t3','type','tt3','}}']));

stijnsanders avatar May 29 '17 09:05 stijnsanders

Thank you, I`ll try.

DDAGitHub avatar May 29 '17 09:05 DDAGitHub

Having a look at this one again, it may be 'data.0.jobs{' instead of 'data[0].jobs{'

stijnsanders avatar May 29 '17 15:05 stijnsanders

Did you solve this? Nothing of the options above work for me, I can't even read data out of an array. Help pls, do you have an example on how to read data from an array?

Skullwing avatar Oct 03 '20 01:10 Skullwing