Couchbase View with changing keys to be emitted in map function

Hi!

I tried to use a secondary index in the following scenario.

There are documents of type ‘car’, with a property ‘price’, e.g.:

conn.set(key='foobar_car', value='{'type': 'car', 'price': 1000}')

I created a view to query the car documents based on the prices:

function (doc, meta) {
    if (doc.type == "car" && doc.price) {
        emit([doc.price], null); 
    }
}

Now I want to use the View to query the cars that have a price between 0 and 1000.

myView.iterate(mapkey_range=[[0], [1000]], reduce=False, stale=False)

This works fine as long as a price of a car does not change, i.e., if I update the price of a car, the query appears to not take this into account, even if I use stale=False. A workaround would be to do the filtering on the client side, but I don’t really like that.

Is this really the intended View behavior? Looking at the documentation, it was not clear to me if the map function is called once per every document, or once per every document change.

Many Thanks!

Hi,

the map function is called once per every document change. It should work. Can you give us more informations?

I have a similar question, let’s say I want to know sum of employee salaries by state, my document is {employeeId:1234, state:IL, salary:12345}, in map function, I will do emit(doc.state, doc.salary}, and I will use _sum reduce function.

now the employee moves to a different state, say, WI, would the reduce function gave the me correct values for IL and WI?