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

Creating views on documents with different number of elements

4 replies [Last post]
  • Login or register to post comments
Tue, 03/05/2013 - 12:34
dragos
Offline
Joined: 03/05/2013
Groups: None

Hi all,
I am new to Couchbase and i have a case when i need to build a view based on some documents (JSON) and this documents don't have the same format.
For example let's say i have the book document which will represent the general format
{
title:"",
author:"",
publisher:"",
edition:"",
notes:"",
borrowers:[{ "user":"John"}, { "user":"Anna"}]
}

When i add new documents to Couchbase i don't have always all the information, like in example 1 and example 2:
example 1:
{
title:"title1",
author:"author1",
publisher:"publisher1",
edition:"edition1",
notes:"notes"
}

example 2:
{
title:"title2",
author:"author1",
publisher:"publisher1"
}

Based on this documents i need to create a view that will be able to search in all fields of the general book document. When i tried the general view didn't show the documents with the different format.

My questions are:
- it is possible to build a view to show all documents ? For example if i provide a ComplexKey.of("author1","publisher1") the results will be: title1 and title2
- is my intention inadequate ?
- what will be a solution to my problem ?

Thank you in advance for the responses.

Top
  • Login or register to post comments
Thu, 03/07/2013 - 00:52
dipti
Offline
Joined: 11/02/2011
Groups:

Hi Dragos,

Not sure I understand your question completely, but here are a few ideas.

If you attribute name matches (it may not exist in all documents but it should match) , then you can simply create a view on the attributes you want and query the view.

in this case, you can do something like

if (doc.author and doc.publisher and doc.title)
{
emit([doc.author,doc.publisher], doc.title);
}

This map function will emit the record for each document, only if title, publisher and author exist. I think that's what you want.

You can then query this using the key=["author1","publisher1"].

The result of this will be all the document IDs that match this key, so 2 JSON docs will be returned, first one with value "title1" and second one with "title2"

hope this helps

- D

Top
  • Login or register to post comments
Sun, 03/10/2013 - 23:17
dragos
Offline
Joined: 03/05/2013
Groups: None

Hi Dipti,
Thx for reply. After spending some more times understanding the NoSql and Couchbase i finally understand how to build the view to match the requirement.

Top
  • Login or register to post comments
Sun, 03/10/2013 - 23:27
dipti
Offline
Joined: 11/02/2011
Groups:

Glad to know you are making good progress. This blog may help you out as well.

http://crate.im/posts/couchbase-views-reddit-data/

Top
  • Login or register to post comments
Sun, 03/10/2013 - 23:31
dragos
Offline
Joined: 03/05/2013
Groups: None

Hi Dipti, can i capture your attention and experience on another bizare problem ?
http://www.couchbase.com/forums/thread/couchbase-2-0-keeps-failing-windo...

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