Ayush
Ayush

Reputation: 3999

How to Write search Query for Couchdb in views where the search object is in array?

I have a documents having the following data.

Document 1.

{
    "_id": "7d7db310ff3acc857c7f301f67979de5",
    "_rev": "1-3ed97634540c35292155ad40b99cafc1",
    "categories": [
        "Computer",
        "Mobile",
        "Automobile",
        "Plumbing"
    ],
    "location": [
        "19.374636239520235",
        "72.82339096069336"
    ],
    "gender": "male",
    "username": "ayushcshah",
    "password": "sokdncx76483ynxwhakdkxfka37",
    "email": "[email protected]"
}

Document 2.

{
    "_id": "8c499253bce69b992ebe795906fac3d3",
    "_rev": "1-ce394be6ab3c79111344f026e1a3adcf",
    "categories": [
        "Automobile"
    ],
    "location": [
        "19.387757921807914",
        "72.82626360654831"
    ],
    "gender": "male",
    "username": "smithabc",
    "password": "adsadgq36eygdas2ygduabhs",
    "email": "[email protected]"
}

I need a VIEW where I would send key as any string and it would find a document where categories array contain the search string and it would send me related user's email address in value.

Example: If I send key as "Mobile" the I would get respected username as value "[email protected]"

key":"Mobile","value":"[email protected]"
key":"Computer","value":"[email protected]"
key":"Automobile","value":"[email protected]"
key":"Automobile","value":"[email protected]"
key":"Plumbing","value":"[email protected]"

Upvotes: 0

Views: 247

Answers (1)

Marcin Skórzewski
Marcin Skórzewski

Reputation: 2854

Following map function should be enough:

function (doc) {
  if (!doc.categories) return;
  for (var c in doc.categories) {
    emit(doc.categories[c], doc.email);
  }
}

Upvotes: 1

Related Questions