Couch DB Mango query to compare 2 fields from the same struct

470 Views Asked by At

I have 2 structs with different "_id" values. I want to get only the struct in which the "costCenterFrom" is not equal to "costCenterTo"

   {
    "_id": "TRAN001",
    "_rev": "3-945670849a142da8d57a79f7c13040dd",
    "actionCode": "Transfer",
    "bankID": "1000003",
    "costCenterFrom": "30000004",
    "costCenterTo": "30000005",
    "effectiveDateOfAction": "30-08-2018",
    "employeeName": "Kumar,Vinoth",
    "transferReportID": "TRAN001",
    "~version": "22:0"
    } 

    {
      "_id": "TRAN002",
      "_rev": "2-1983dcdedc144d75b14c1ef73771fc42",
      "actionCode": "Transfer",
      "bankID": "1000004",
      "costCenterFrom": "30000002",
      "costCenterTo": "30000002",
      "effectiveDateOfAction": "31-08-2018",
      "employeeName": "Kumar",
      "transferReportID": "TRAN002",
      "~version": "12:0"
      } 

Please find the query i tried

    queryStringTrans := fmt.Sprintf("{\"selector\":{\"transferReportID\": 
     {\"$ne\":\"%s\"}}}", "null")
    queryResultsTrans, err := getQueryResultForQueryString(stub, 
    queryStringTrans)

The above query (queryResultsTrans) will give all the record that has "transferReportID" field.

var costcenterFrom string
var costcenterTo string

var resultsdummy []KeyRecordTransfer
err = json.Unmarshal([]byte(queryResultsTrans), &resultsdummy)

for _, trasnresult := range resultsdummy {
fmt.Println(" resultsdummy Record : ", trasnresult)
costcenterFrom = trasnresult.Record.CostCenterFrom
costcenterTo = trasnresult.Record.CostCenterTo

if (costcenterFrom != costcenterTo) {
    fmt.Println("costcenterFrom && costCenterTo : ", costcenterFrom, 
   costcenterTo)
}
//var costcenterFrom = resultsdummy[0].Record.CostCenterFrom
//var costcenterTo = resultsdummy[0].Record.CostCenterTo
}

//query to get the Transfer Reports

The below query which i tried to compare the "costCenterFrom" and "costCenterTo" fields

queryString := fmt.Sprintf("{\"selector\":{\"$and\":[{\"transferReportID\": 
{\"$ne\":\"%s\"}},{\"%s\":{\"$ne\":\"%s\"}},{\"effectiveDateOfAction\": 
{\"$gt\":\"%s\"}}]}}","null",costcenterFrom,costcenterTo,"30-07-2018")
queryResults, err := getQueryResultForQueryString(stub, queryString) 

Im not getting the expected result. Could you please help me in getting the query to compare the fields inside the same struct?

My raw query:

  {
    "selector": {
    "$and": [
       {
        "transferReportID": {
           "$ne": null
        }
      },
      {
        "costcenterFrom": {
           "$ne": "costcenterTo"
        }
       },
      {
         "effectiveDateOfAction": {
            "$gt": "30-07-2018"
         }
      }
     ]
     }
    } 
1

There are 1 best solutions below

2
On

I tried your query and you have some typos.

So in your query, you have a missing camel case: costcenterFrom From my tests, the $ne seems to check if the field exist.

Other comment, you can't compare other object fields. I see that you're trying to compare the costCenterTo property with the costCenterFrom but that's not supported by mango at the moment.