user18862346
user18862346

Reputation:

Retrieve data from couchdb (how to verify that a value is in a database)

how can I retrieve data from a couchdb database, not in a document of a database but in all the documents of the database. I already verified if a value was in a document but I want now to verify if a value is in a special database.

Upvotes: 0

Views: 325

Answers (1)

xpqz
xpqz

Reputation: 3737

It's hard to be specific here, since you're not providing any example data. Do you mean if a particular value exists anywhere in any document, or if a specific field holds a specific value in any document?

If you mean the former, I'd advice you to come up with a better data model. So let's focus on the latter. Let's say your documents contain the field called special, and you want to know if any such documents has the value 99 in this field:

{
    ...
    "special": 99
}

Create a view keyed on the value:

function(doc) {
  if (doc && doc.special) {
    emit(doc.special, null);
  }
}

Now you can check if documents exist in this database for values of the special field:

# Look for the value 99
% acurl 'https://XYZ.cloudant.com/aaa/_design/test/_view/special?key=99'  
{"total_rows":3,"offset":2,"rows":[
   {"id":"a3c424e99f3cc9988a2553bb680ac7f8","key":99,"value":null}
]}

In my databse aaa there is one document, with the id a3c424e9... that has the value 99 in the special field. This is a so-called reverse index.

Upvotes: 2

Related Questions