finding a doc based on 2 values
hi,
i am trying to store some chat history in the couch db for future retrieval by user.
the doc has a the following fields.
doc_owner and chat_partner
this doc is unique so there can be only one doc for the doc_owner and chat_partner pair
how can i retrieve the doc based on these 2 values since i dont know the key which is a
auto increment int. in a view i can create a view based on doc_owner but how can i get the
one which has a certain chat_partner without having to get all and loop thru the return
of view and look at value ?
in SQL it would be a simple where doc_owner =x and chat_partner = z
not sure but i guess i am missing something..
here is what i use to create view "emit(doc.owner.id, doc.chat_partner.id, null);"
here is the output of view without key filter
{"total_rows":3,"rows":[
{"id":"chat_1234","key":"4","value":"8"},
{"id":"chat_1235","key":"4","value":"9"},
{"id":"chat_1236","key":"9","value":"8"}
]
}
if i use &key=[4] or key=4 or key=["4"] or key=[4,8] or key=["4","8"] i always get no data returned
so not sure what i am doing wrong
Sorry typo in the earlier post.
needs to be emit ([doc.doc_owner, doc.chat_partner], null)
In this case, the key is an array [doc.doc_owner, doc.chat_partner] , the value is null.
BTW, here's a more complex example as well. http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-sampl...
thanks, i didnt see the [] in your first post..
can you explain a bit more what you mean by
"For in this example, you have to have "Joe" and if you do range searches, the start and endkey will need to be ordered as well."
in my case the key is owner.id, chat_partner.id and to query i use key=["$owner.id","$chat_partner.id"]
what else do i need to watch out for ?
If you are always looking for a specific combination,
for example ["a", "b"] - you should be fine.
But you cannot arbitrarily search of just the second attribute (ignoring the first) attribute, you cannot do it, with this view. You will need to create a view on just the second attribute.
Give it a try and play around a bit, I think you'll say what I mean.
sounds good. hope this helped !
You can have complex keys in your view. For example, you can emit ([doc.doc_owner, doc.chat_partner], null)
When you query, you can use a range lookup or key lookup, something like
http://mycouchserver:8092/mybucket/_design/dev_mydesigndoc/_view/myview?key=["Joe","Bob"]
remember that indexes are sorted by the order of the attributed emitted in the Map function "key".
For in this example, you have to have "Joe" and if you do range searches, the start and endkey will need to be ordered as well.
This blog may help understanding collation better: http://blog.couchbase.com/understanding-grouplevel-view-queries-compound...