简体   繁体   中英

How to merge data from another collection to an array of keys or ids?

Here is the problem I am facing:

I am using arangob 3.7 and arangojs driver.

I have following collections:

collection A {    _key,    data }

collection B {    _key,    aDataList[A._key] }

I have tried the following

 FOR bdoc IN B  
 FILTER bdoc._key == "some_key" 
 FOR adoc IN A 
 FILTER adoc._key IN bdoc.aDataList[*]  
 RETURN MERGE(bdoc, adoc)

This query returns the objects which falls in to the criteria specified.

But the problem I am facing is the bdoc.aDataList[] order is not same as the one in the actual B document collection. Lets say here is the sample list:

bdoc.aDataList[    1,    2,    3 ]

How it need to be updated?

bdoc.aDataList[
   {
     "_key" : 1,
     "data"  : "somedata"
   },
      {
     "_key" : 2,
     "data"  : "somedata"
   },
   {
     "_key" : 3,
     "data"  : "somedata"
   }
]

How to properly replace the aDataList[A.Key] with aDataList[A] values using a single aql query?

Any help would be appreciated

I have found an answer:)

FOR bDoc IN B 
FILTER bDoc.key == “somekey” 
LET finalData = ( FOR bDocItem IN bDoc.aDataList
FOR aDoc IN A
FILTER bDocItem[“_key”] == aDoc._key 
RETURN aDoc)
RETURN { "_key" : bDoc.key, aDataList: finalData }

Instead of traversing keys of A, I traverse through the array. Thus order is preserved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM