Hi All,
I am stuck to find the right solution of the below use case.
I have existing documents in “travel-sample” bucket as below:
{
“travel-sample”: {
“id”:1001,
“travelid”: “travel_1001”,
“callsign”: “MILE-AIR”,
“country”: “United States”,
“iata”: “Q5”,
“icao”: “MLA”,
“name”: “40-Mile Air”,
“type”: “airline”
}
},
{
“travel-sample”: {
“id”:1002,
“travelid”: “travel_1003”,
“callsign”: “TXW”,
“country”: “United States”,
“iata”: “TQ”,
“icao”: “TXW”,
“name”: “Texas Wings”,
“type”: “airline”
}
}.
{
“travel-sample”: {
“id”:1003,
“travelid”: “travel_1003”,
“callsign”: “TXW”,
“country”: “United States”,
“iata”: “TQ”,
“icao”: “TXW”,
“name”: “Texas Wings”,
“type”: “airline”
}
}
Value of travelid is derived by prefixing with static text “travel_” to the value of id. As shown in the above example, if value of id is 1001, travelid will be travel_1001
The document key is on field travelid.
I want to insert a new field called “p_id”. The value of “p_id” will populated through named parameter (as suggested in Update query to add new attribute and its value in existing document).
\set -$inputcsv {“1001”:“2121”, “1002”:“2222”, “1003”:“2323”} ;
Additionally, I would need to update the value of travelid as travel_<p_id>, e.g. travel_2121. But, as this is the key field, though the value can be updated, the document key still retains the old value.
One of the solution I can think of is to create a new document with the new value of travelid as document key, i.e.
Desired Output
{
“travel-sample”: {
“id”:1001,
“p_id”:2121,
“travelid”: “travel_2121”,
“callsign”: “MILE-AIR”,
“country”: “United States”,
“iata”: “Q5”,
“icao”: “MLA”,
“name”: “40-Mile Air”,
“type”: “airline”
}
},
{
“travel-sample”: {
“id”:1002,
“p_id”:2222,
“travelid”: “travel_2222”,
“callsign”: “TXW”,
“country”: “United States”,
“iata”: “TQ”,
“icao”: “TXW”,
“name”: “Texas Wings”,
“type”: “airline”
}
}.
{
“travel-sample”: {
“id”:1003,
“p_id”:2323,
“travelid”: “travel_2323”,
“callsign”: “TXW”,
“country”: “United States”,
“iata”: “TQ”,
“icao”: “TXW”,
“name”: “Texas Wings”,
“type”: “airline”
}
}
Not able to figure out the right query to meet this requirement. Please help
Thank you,
Regards,
Jayant