Search code examples
couchdbcouchdb-mango

How to find document in CouchDB where document has an array that contains a specific string?


I am just getting started working with CouchDB.

We have a database where the documents look like:

  "_id": "43538c8409dd",
  "_rev": "123",
  "audit": false,
  "members": {
    "users": [
      {
        "dn": "cn=newuser1,ou=people,dc=foo,dc=com",
        "dateAdded": "2020-12-02T16:52:41:748Z"
      },
      {
        "dn": "cn=newuser2,ou=people,dc=foo,dc=com",
        "dateAdded": "2020-12-02T16:52:41:748Z"
      }
    ],
    "groups": []
  },
.
.
.
}

I am trying to find all documents in the database that contain (for example) "dn" with value "cn=newuser1,ou=people,dc=foo,dc=com".

I have tried a selector like:

   "selector": {
      "users": {
         "$elemMatch": {
            "dn": "cn=newuser1,ou=people,dc=foo,dc=com"
         }
      }
   }
}

I know that there are several documents that should match, but I am getting "No Documents Found".

Can someone tell me why this search is not working?

Thanks, Jim


Solution

  • The rather important members field is not being specified. This is likely the query intended

    {
       "selector": {
          "members.users": {
             "$elemMatch": {
                "dn": "cn=newuser1,ou=people,dc=foo,dc=com"
             }
          }
       }
    }