Creating views on documents with different number of elements
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.
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.
Glad to know you are making good progress. This blog may help you out as well.
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...
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