Cbq null handling if response is null while iterating over loop

Hi,

I am using cbq command inside the loop and iterating with some value. In some cases there is no data on couchbase cluster hence result object is returning empty, but in that case null is getting stored inside the file.Is there a way to write the query in such a way if result is empty, no value should be stored in the file and then keep iterating.

for price in "${priceIds[@]}"
do
  
  price=$(echo "$price" | sed 's/^[ \n]*//;s/[ \n]*$//')
  printf "$price"
  
  cbq -u Administrator -p ${password}  -e "http://localhost:8093"  --script="select * FROM \`com.asec.test\` where documentId==\"${price}\";" | jq -c '.results[0]."com..src.test"' >> output.json
  
done

In some case below is the response:

 select * FROM `com.src.test` where documentId=="12345";
{
    "requestID": "3f5e13c8-c7f8-4df5-a23e-27125c768a86",
    "signature": {
        "*": "*"
    },
    "results": [
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "2.292376ms",
        "executionTime": "2.116336ms",
        "resultCount": 0,
        "resultSize": 0
    }
}

and in some case result object is populated.

That is not cbq issue. The way you process jq . Try | jq -c ‘.results[0].“com…src.test” | select(length > 0)’

cbq -u Administrator -p ${password}  -e "http://localhost:8093"  --script="select * FROM \`com.asec.test\` where documentId==\"${price}\";" | jq -c '.results[0]."com..src.test" | select(length > 0)' >> output.json
1 Like