OrientDB批量更新后如何返回指定属性而非RecordLink?
Solution for OrientDB Batch Update Returning Name-RID Map
The issue with your current batch command is that you're returning an array of full records (represented as OrientDBRecordLink objects), which don't directly give you the key-value format you want. Instead, you need to build a map incrementally in your batch script that maps each updated name to its RID.
Here's the corrected batch command that will return the exact format you're looking for:
begin; -- Initialize an empty map to store our name-RID pairs let result = {}; -- First update: modify the record and capture the updated version let a0 = update #44:845 merge {"name": "B4"} return after; -- Add the name and RID to our result map (convert RID to string for the desired format) let result = result.put($a0.name, $a0.@rid.toString()); -- Second update let a1 = update #44:849 merge {"name": "Name4"} return after; let result = result.put($a1.name, $a1.@rid.toString()); -- Repeat the above two lines for all your updates... -- Commit the transaction with retry logic commit retry 100; -- Return the final map return result;
Key Details:
- Initializing the Map: We start with
let result = {}to create an empty dictionary that will hold our key-value pairs. - Capturing Updated Records: Each
update ... return afterstatement stores the fully updated record in a variable (like$a0). This gives us access to both the newnameproperty and the record's@rid. - Building the Map: Using
result.put(key, value), we add each entry to the map. We calltoString()on@ridbecause it's an ORecordId object by default—converting it to a string gives you the familiar#44:845format. - Commit and Return: After all updates are processed, we commit the transaction and return the completed map.
Important Notes:
- If multiple updates set the same
namevalue, the last one will overwrite the previous entry in the map (since map keys must be unique). - The
mergeoperation ensures thenameproperty is set (even if it didn't exist before), so you don't have to worry about missing values in the result.
内容的提问来源于stack exchange,提问作者Diemo
相关产品推荐
相关产品推荐

