Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | Couchbase | Couchbase Server 2.0

finding a doc based on 2 values

7 replies [Last post]
  • Login or register to post comments
Wed, 01/30/2013 - 11:48
makeawish
Offline
Joined: 12/03/2012
Groups: None

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

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:11
dipti
Offline
Joined: 11/02/2011
Groups:

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...

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:10
makeawish
Offline
Joined: 12/03/2012
Groups: None

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

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:12
dipti
Offline
Joined: 11/02/2011
Groups:

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.

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:17
dipti
Offline
Joined: 11/02/2011
Groups:

BTW, here's a more complex example as well. http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-sampl...

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:19
makeawish
Offline
Joined: 12/03/2012
Groups: None

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 ?

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:22
dipti
Offline
Joined: 11/02/2011
Groups:

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.

Top
  • Login or register to post comments
Wed, 01/30/2013 - 14:50
dipti
Offline
Joined: 11/02/2011
Groups:

sounds good. hope this helped !

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker