Tiny Dev
Tiny Dev

Reputation: 31

Advanced Queries - Partial Match

I'm trying to figure out the best way to query very variable data in a Cloudant/CouchDB database. The only way for me to explain it is by showing an example:

Database example

    {
        "_id": "1",
        "query-able_values": {
            "value-type-b": "some value",
            "value-type-c": "some other value",
            "value-type-d": "another value",
            "value-type-f": "yet another value",
        }
    }

    {
        "_id": "2",
        "query-able_values": {
            "value-type-a": "value",
            "value-type-c": "some other value",
            "value-type-d": "another value"
        }
    }

    {
        "_id": "3",
        "query-able_values": {
            "value-type-a": "some value",
            "value-type-e": "some other one",
        }
    }

Query Example

Now suppose the user performs the following query:

    "selector": {
        "query-able_values.value-type-a": "value",
        "query-able_values.value-type-b": "a value not present in the database",
        "query-able_values.value-type-c": "some other value",
        "query-able_values.value-type-d": "another value",
        "query-able_values.value-type-e": "a value not present in the database",
        "query-able_values.value-type-f": "a value not present in the database"
    }

What do I need to change so that the query will hit database entry with id 2?

EDIT I wasn't entirely clear in my original question. The user is completely agnostic of what is in the database and thus, of which values are relevant. The user simply has values a through f. I'm trying to figure out a way to get the "closest matching database entry".

Upvotes: 1

Views: 174

Answers (1)

uminder
uminder

Reputation: 26150

According to CouchDB selector syntax, this could be written as follows.

"selector": {
    "query-able_values": {
        "value-type-a": "value",
        "value-type-b": { "$exists": false },
        "value-type-c": "some other value",
        "value-type-d": "another value",
        "value-type-e": { "$exists": false },
        "value-type-f": { "$exists": false }
    }
}

or with explicit equality operator.

"selector": {
    "query-able_values": {
        "value-type-a": { "$eq": "value"},
        "value-type-b": { "$exists": false },
        "value-type-c": { "$eq": "some other value"},
        "value-type-d": { "$eq": "another value"},
        "value-type-e": { "$exists": false },
        "value-type-f": { "$exists": false }
    }
}

Upvotes: 1

Related Questions