选择器是数组时如何搜索值 [英] how to search for values when the selector is an array

查看:61
本文介绍了选择器是数组时如何搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的沙发数据库上有以下json文档。

I have the following json document on my couch db.

{ 
  {
      "_id": "someid|goes|here",
      "collected": {
          "tags": ["abc", "def", "ghi"]
      }
  },
  {
      "_id": "someid1|goes|here",
      "collected": {
           "tags": ["abc", "klm","pqr"]
      },
  },
  {
      "_id": "someid2|goes|here",
      "collected": {
           "tags": ["efg", "hij","klm"]
      },
  }
}

如何获取 collected.tags 包含 abc 的记录?到目前为止,我尝试过的是

How can I get the records where collected.tags contains abc? What I have tried so far is

{
   "selector": {
      "collected.tags": {
         "$regex": "abc"
      }
   }
}


推荐答案

如果我正确理解了您正在查询的3个不同文档中的内容。根据CouchDb文档,使用 $ elemMatch 运算符,可以返回所有文档,其中至少有一个元素根据指定条件匹配。如果是这样,请使用以下查询过滤出文本 abc:

If I've understood correctly you are querying this from 3 different documents. Using the $elemMatch operator as per CouchDb Documentation it's possible to return all documents where at least one element that is matched based on the specified criteria. If so use the following query to filter out the text "abc":

{
  "selector": {
    "collected.tags": {
      "$elemMatch": {
        "$regex": "abc"
      }
    }
  }
}

希望这会有所帮助。 :)

Hope this helps out. :)

这篇关于选择器是数组时如何搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆