How to find by attribute starting with $

旧城冷巷雨未停 提交于 2020-01-24 00:36:10

问题


I try to query couchdb server with _find endpoint by attribute, starting with '$' ($ref in my case). But server always returns empty document set.

  1. I have couchdb documents like this:
{
  "_id": "59bb208006149f50bb32f76f4900ccfa",
  "_rev": "1-99022821cc2bb3ab0bdd84ab98b55828",
  "contents": {
    "eClass": "auth#//User",
    "name": "SuperAdminUser",
    "roles": [
      {
        "eClass": "auth#//Role",
        "$ref": "59bb208006149f50bb32f76f4900c962?rev=1-24d9469afe50f162e473b09fdbd95154#/"
      }
    ],
    "email": "admin@mydomain.ru",
  }
}
  1. I try to query this document like this:
{
    "contents": {
        "eClass": "auth#//User",
        "roles": {
            "$elemMatch": {
                "eClass": {"$regex": ".*auth#//Role"},
                "$ref": {"$regex": "^59bb208006149f50bb32f76f4900c962.*"}
            }
        }
    }   
}

but no results returned.

  1. Query like
{
    "contents": {
        "eClass": "auth#//User",
        "roles": {
            "$elemMatch": {
                "eClass": {"$regex": ".*auth#//Role"}
            }
        }
    }   
}

works as expected.

It seems the mango server did not recognize attributes like $ref.

I tried to escape attribute with "\$ref" with no success. (not true!!!, see update)

Is there any workarounds to query such attributes like $ref?


回答1:


Note: OP provided this answer as an update to the question. I've moved it here to conform to site standards.


This query works.
{
   "selector": {
      "contents": {
         "eClass": "auth#//User",
         "roles": {
            "$elemMatch": {
               "eClass": {
                  "$regex": ".*auth#//Role"
               },
               "\\$ref": {
                  "$regex": ".*59bb208006149f50bb32f76f4900c962.*"
               }
            }
         }
      }
   }
}

So just use "\\$ref" escape style.



来源:https://stackoverflow.com/questions/57035213/how-to-find-by-attribute-starting-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!