Couch DB Mango查询以比较来自同一结构的2个字段

时间:2018-11-15 08:08:36

标签: couchdb hyperledger-fabric mangodb couchdb-mango

我有2个具有不同“ _id”值的结构。我只想获取其中“ costCenterFrom”不等于“ 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"
      } 

请找到我尝试过的查询

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

上面的查询(queryResultsTrans)将给出所有具有“ transferReportID”字段的记录。

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

以下我尝试比较“ costCenterFrom”和“ costCenterTo”字段的查询

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) 

我没有得到预期的结果。您能否帮助我获取查询以比较同一结构中的字段?

我的原始查询:

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

1 个答案:

答案 0 :(得分:1)

我尝试了您的查询,但是您有一些错字。

因此,在您的查询中,您缺少一个骆驼案: cost c enterFrom 根据我的测试, $ ne 似乎正在检查该字段是否存在。

其他注释,您无法比较其他对象字段。我发现您正在尝试将costCenterTo属性与costCenterFrom进行比较,但芒果目前不支持。