You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Markus Burrer <mb...@embedit.de> on 2011/09/23 11:14:55 UTC

get by property

Hi, I have a document like this

|{
    "|_id|":|"ATmega8A-PU"|,
    "|_rev|":|"5-76258a717723df03afe068a7e001d108"|,
    "|name|":|{
        "|de|":|"ATmega8A-PU"|,
        "|en|":|"ATmega8A-PU"|
    }|,
    "|properties|":|{
        "|Flash|":|"8kByte"|,
        "|RAM|":|"1kByte"|,
        "|Eeprom|":|"512Byte"|,
        "|Package|":|"DIP28S"|
    }|,
    "|record_type|":|"article"|
}|

I want to get all documents with the same property, for example with the 
package "DIP28S" or all docs with the property "RAM":"1kByte".

How should the view look like?

Kind Regards
Markus

Re: get by property

Posted by Markus Burrer <mb...@embedit.de>.
Am 23.09.2011 16:12, schrieb Sean Copenhaver:
> Just a nitpick really, but you don't need to emit any value if you are only
> interested in the _id or retrieving the full document.
>
I know. This is just for testing.


Re: get by property

Posted by Sean Copenhaver <se...@gmail.com>.
Just a nitpick really, but you don't need to emit any value if you are only
interested in the _id or retrieving the full document.

As you can see in the results you lists the document's id is already given
and you can specify the query string parameter "include_docs=true" to fetch
the full doc with your results.

I thought I would add that in case you were unaware.

On Fri, Sep 23, 2011 at 9:42 AM, Markus Burrer <mb...@embedit.de> wrote:

> Am 23.09.2011 14:10, schrieb Nils Breunese:
>
>  You could create views for every property you want to be able to use as a
>> lookup key (by_flash, by_ram, by_eeprom, by_package) and emit the property
>> value as the key: emit(property_value, null); Then you can query the view
>> for the property you're looking for, e.g /db/_design/ddoc/_view/by_**
>> flash?key="8kbyte"
>>
>> Or you could create a single view which indexes all properties with their
>> values by emitting the property key and value as a complex key:
>> emit([property_key, property_value], null); Then you can query the view like
>> this: /db/_design/ddoc/_view/all_**properties?key=["Flash","**8kbyte"]
>>
> Hi Nils,
> thanks. I prefer the second version. My map function now looks like this.
> Is this correct?
>
> function(doc) {
>
>  if(doc.record_type=="article")**{
>
>    for(var key in doc.properties){
>
>      emit([key, doc.properties[key]], doc._id);
>
>    }
>
>  }
>
> }
>
>
> The query /test/_design/find/_view/by_**property?key=["Flash","8kByte"**]
> returns the result
>
> {"total_rows":8,"offset":3,"**rows":[
> {"id":"ATmega8A-PU","key":["**Flash","8kByte"],"value":"**ATmega8A-PU"}
> ]}
>
> Looks good for me.
>



-- 
“The limits of language are the limits of one's world. “ - Ludwig von
Wittgenstein

"Water is fluid, soft and yielding. But water will wear away rock, which is
rigid and cannot yield. As a rule, whatever is fluid, soft and yielding will
overcome whatever is rigid and hard. This is another paradox: what is soft
is strong." - Lao-Tzu

Re: get by property

Posted by Markus Burrer <mb...@embedit.de>.
Am 23.09.2011 14:10, schrieb Nils Breunese:
> You could create views for every property you want to be able to use as a lookup key (by_flash, by_ram, by_eeprom, by_package) and emit the property value as the key: emit(property_value, null); Then you can query the view for the property you're looking for, e.g /db/_design/ddoc/_view/by_flash?key="8kbyte"
>
> Or you could create a single view which indexes all properties with their values by emitting the property key and value as a complex key: emit([property_key, property_value], null); Then you can query the view like this: /db/_design/ddoc/_view/all_properties?key=["Flash","8kbyte"]
Hi Nils,
thanks. I prefer the second version. My map function now looks like 
this. Is this correct?

function(doc) {

   if(doc.record_type=="article"){

     for(var key in doc.properties){

       emit([key, doc.properties[key]], doc._id);

     }

   }

}


The query /test/_design/find/_view/by_property?key=["Flash","8kByte"] 
returns the result

{"total_rows":8,"offset":3,"rows":[
{"id":"ATmega8A-PU","key":["Flash","8kByte"],"value":"ATmega8A-PU"}
]}

Looks good for me.

RE: get by property

Posted by Nils Breunese <N....@vpro.nl>.
You could create views for every property you want to be able to use as a lookup key (by_flash, by_ram, by_eeprom, by_package) and emit the property value as the key: emit(property_value, null); Then you can query the view for the property you're looking for, e.g /db/_design/ddoc/_view/by_flash?key="8kbyte"

Or you could create a single view which indexes all properties with their values by emitting the property key and value as a complex key: emit([property_key, property_value], null); Then you can query the view like this: /db/_design/ddoc/_view/all_properties?key=["Flash","8kbyte"]

Nils.
________________________________________
Van: Markus Burrer [mb@embedit.de]
Verzonden: vrijdag 23 september 2011 11:14
Aan: user@couchdb.apache.org
Onderwerp: get by property

Hi, I have a document like this

|{
    "|_id|":|"ATmega8A-PU"|,
    "|_rev|":|"5-76258a717723df03afe068a7e001d108"|,
    "|name|":|{
        "|de|":|"ATmega8A-PU"|,
        "|en|":|"ATmega8A-PU"|
    }|,
    "|properties|":|{
        "|Flash|":|"8kByte"|,
        "|RAM|":|"1kByte"|,
        "|Eeprom|":|"512Byte"|,
        "|Package|":|"DIP28S"|
    }|,
    "|record_type|":|"article"|
}|

I want to get all documents with the same property, for example with the
package "DIP28S" or all docs with the property "RAM":"1kByte".

How should the view look like?

Kind Regards
Markus
------------------------------------------------------------------------
 VPRO   www.vpro.nl
------------------------------------------------------------------------