You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Matt Villiers <ma...@yahoo.fr> on 2010/01/22 16:38:00 UTC
view syntax for retrieving object without a field
I'm prototyping couchdb for a large application (for a possible migration from an oracle system).
I'l like to query my objects to get those _without_ a certain key.
My data:
{'id':'2','Type':'Article', country:'USA'}
{'id':'3','Type':'Article', country:'USA'}
{'id':'4','Type':'Article',}
{'id':'5','Type':'Article', country:null}
I'd like to get the object number 4 (no field 'country').
I tried with this view:
function(doc) {
if (doc.Type == "Article") {
emit('country' in doc, doc);
}
}
With False as the request key but with no luck ...
Is that feasible with couchdb ? Could you advise ?
Best,
Matt
Re: view syntax for retrieving object without a field
Posted by Chris Anderson <jc...@apache.org>.
On Fri, Jan 22, 2010 at 7:38 AM, Matt Villiers <ma...@yahoo.fr> wrote:
> I'm prototyping couchdb for a large application (for a possible migration from an oracle system).
>
> I'l like to query my objects to get those _without_ a certain key.
> My data:
> {'id':'2','Type':'Article', country:'USA'}
> {'id':'3','Type':'Article', country:'USA'}
> {'id':'4','Type':'Article',}
> {'id':'5','Type':'Article', country:null}
>
> I'd like to get the object number 4 (no field 'country').
>
> I tried with this view:
>
> function(doc) {
> if (doc.Type == "Article") {
> emit('country' in doc, doc);
> }
> }
>
function(doc) {
if (doc.Type == "Article") {
if (doc.country) {
emit(doc.country, null);
} else {
emit("--no-country", null);
}
}
};
instead of "--no-country" you could emit false, but currently if there
is no .country field then CouchDB will just skip the row.
there are plans to make it so that attempts to emit undefined will be
coerced to null (this is in keeping with the ecma5 standard) but that
won't change things materially for you.
Chris
> With False as the request key but with no luck ...
>
> Is that feasible with couchdb ? Could you advise ?
>
> Best,
>
> Matt
>
>
>
>
--
Chris Anderson
http://jchrisa.net
http://couch.io
Re: view syntax for retrieving object without a field
Posted by Markus Jelsma <ma...@buyways.nl>.
Hello Brian,
Funny, i first used your approach but for some reason it didn't work, it
must have been me writing a typo or juggling thing about. Thanks for
pointing it out!
Brian Candler said:
> On Mon, Jan 25, 2010 at 11:51:05AM -0500, eric casteleijn wrote:
>> >function (doc)
>> >{
>> > var fields = new Array("name", "country");
>> >
>> > for (idx in fields)
>> > {
>> > if (typeof eval("doc." + fields[idx]) == "undefined")
>
> I must be missing a Javascript subtlety here. What's wrong with
>
> if (typeof(doc[fields[idx]]) === 'undefined')
>
> ?
Re: view syntax for retrieving object without a field
Posted by Brian Candler <B....@pobox.com>.
On Mon, Jan 25, 2010 at 11:51:05AM -0500, eric casteleijn wrote:
> >function (doc)
> >{
> > var fields = new Array("name", "country");
> >
> > for (idx in fields)
> > {
> > if (typeof eval("doc." + fields[idx]) == "undefined")
I must be missing a Javascript subtlety here. What's wrong with
if (typeof(doc[fields[idx]]) === 'undefined')
?
Re: view syntax for retrieving object without a field
Posted by eric casteleijn <er...@canonical.com>.
> Let's provide you with a better alternative, you need only one view to handle
> all possible negations, you only need to specify on which negations you want
> to select, check it out!
>
> function (doc)
> {
> var fields = new Array("name", "country");
>
> for (idx in fields)
> {
> if (typeof eval("doc." + fields[idx]) == "undefined")
> {
> emit(fields[idx], null);
> }
> }
> }
>
I think it's perfectly safe in this case, but in general I would not
encourage the use of eval, as it might trip up inexperienced developers
into creating serious security issues in their applications.
Almost always you can solve the problem without eval, as I think is the
case here, at the expense of a little more code. Unfortunately the proof
is too large to fit in the margin. ;)
Re: view syntax for retrieving object without a field
Posted by Markus Jelsma <ma...@buyways.nl>.
Hello Adam,
Thanks for your suggestion, the "undefined" indeed doesn't work as
expected. Using undefined (without quotes) does, the same goes for using
=== void 0.
I updated the wiki with your suggestion, thanks!
Cheers,
Adam Wolff said:
> In your first example, you wrote
>
> if (doc.field == "undefined")
>
>
> I think you meant
>
> if (typeof doc.field == "undefined")
>
>
> But I usually write
>
> if (doc.field === void 0)
>
> if I really want to check for undefined, since "undefined" is not a js
> keyword; it's just a conventional variable that has no value.
>
> A
>
> On Fri, Jan 22, 2010 at 12:38 PM, Markus Jelsma <ma...@buyways.nl>
> wrote:
>
>> Hello,
>>
>>
>> For the sake of clarity and reusability i have added this problem to
>> the wiki [1]. Please check and modify or extend if there are some
>> flaws or if something is missing.
>>
>> [1]:
>> http://wiki.apache.org/couchdb/View_Snippets#documents_without_a_field
>>
>>
>> Cheers,
>>
>>
>> Markus Jelsma said:
>> > Hi,
>> >
>> >
>> > I have some documents, all with a `name` field and some with a
>> country field defined. Using the map function below i can retrieve
>> all documents that have no country field defined.
>> >
>> > function (doc)
>> > {
>> > var
>> >
>> > if (doc.country == "undefined")
>> > {
>> > emit(doc.name, null);
>> > }
>> > }
>> >
>> > I do believe this could be very unusable in many situations,
>> especially if you need to retrieve documents based on the absence
>> of a lot of different fields.
>> >
>> > Let's provide you with a better alternative, you need only one view
>> to handle all possible negations, you only need to specify on which
>> negations you want to select, check it out!
>> >
>> > function (doc)
>> > {
>> > var fields = new Array("name", "country");
>> >
>> > for (idx in fields)
>> > {
>> > if (typeof eval("doc." + fields[idx]) == "undefined")
>> > {
>> > emit(fields[idx], null);
>> > }
>> > }
>> > }
>> >
>> > Now you can just query the view with your key and retrieve all
>> documents
>> > without that field.
>> >
>> > Hope this helps.
>> >
>> >
>> > Cheers,
Re: view syntax for retrieving object without a field
Posted by Adam Wolff <aw...@gmail.com>.
In your first example, you wrote
if (doc.field == "undefined")
I think you meant
if (typeof doc.field == "undefined")
But I usually write
if (doc.field === void 0)
if I really want to check for undefined, since "undefined" is not a js
keyword; it's just a conventional variable that has no value.
A
On Fri, Jan 22, 2010 at 12:38 PM, Markus Jelsma <ma...@buyways.nl> wrote:
> Hello,
>
>
> For the sake of clarity and reusability i have added this problem to the
> wiki [1]. Please check and modify or extend if there are some flaws or if
> something is missing.
>
> [1]:
> http://wiki.apache.org/couchdb/View_Snippets#documents_without_a_field
>
>
> Cheers,
>
>
> Markus Jelsma said:
> > Hi,
> >
> >
> > I have some documents, all with a `name` field and some with a country
> > field defined. Using the map function below i can retrieve all
> > documents that have no country field defined.
> >
> > function (doc)
> > {
> > var
> >
> > if (doc.country == "undefined")
> > {
> > emit(doc.name, null);
> > }
> > }
> >
> > I do believe this could be very unusable in many situations, especially
> > if you need to retrieve documents based on the absence of a lot of
> > different fields.
> >
> > Let's provide you with a better alternative, you need only one view to
> > handle all possible negations, you only need to specify on which
> > negations you want to select, check it out!
> >
> > function (doc)
> > {
> > var fields = new Array("name", "country");
> >
> > for (idx in fields)
> > {
> > if (typeof eval("doc." + fields[idx]) == "undefined")
> > {
> > emit(fields[idx], null);
> > }
> > }
> > }
> >
> > Now you can just query the view with your key and retrieve all documents
> > without that field.
> >
> > Hope this helps.
> >
> >
> > Cheers,
>
>
>
>
Re: view syntax for retrieving object without a field
Posted by Markus Jelsma <ma...@buyways.nl>.
Hello,
For the sake of clarity and reusability i have added this problem to the
wiki [1]. Please check and modify or extend if there are some flaws or if
something is missing.
[1]: http://wiki.apache.org/couchdb/View_Snippets#documents_without_a_field
Cheers,
Markus Jelsma said:
> Hi,
>
>
> I have some documents, all with a `name` field and some with a country
> field defined. Using the map function below i can retrieve all
> documents that have no country field defined.
>
> function (doc)
> {
> var
>
> if (doc.country == "undefined")
> {
> emit(doc.name, null);
> }
> }
>
> I do believe this could be very unusable in many situations, especially
> if you need to retrieve documents based on the absence of a lot of
> different fields.
>
> Let's provide you with a better alternative, you need only one view to
> handle all possible negations, you only need to specify on which
> negations you want to select, check it out!
>
> function (doc)
> {
> var fields = new Array("name", "country");
>
> for (idx in fields)
> {
> if (typeof eval("doc." + fields[idx]) == "undefined")
> {
> emit(fields[idx], null);
> }
> }
> }
>
> Now you can just query the view with your key and retrieve all documents
> without that field.
>
> Hope this helps.
>
>
> Cheers,
Re: view syntax for retrieving object without a field
Posted by Markus Jelsma <ma...@buyways.nl>.
Hi,
I have some documents, all with a `name` field and some with a country field
defined. Using the map function below i can retrieve all documents that have
no country field defined.
function (doc)
{
var
if (doc.country == "undefined")
{
emit(doc.name, null);
}
}
I do believe this could be very unusable in many situations, especially if you
need to retrieve documents based on the absence of a lot of different fields.
Let's provide you with a better alternative, you need only one view to handle
all possible negations, you only need to specify on which negations you want
to select, check it out!
function (doc)
{
var fields = new Array("name", "country");
for (idx in fields)
{
if (typeof eval("doc." + fields[idx]) == "undefined")
{
emit(fields[idx], null);
}
}
}
Now you can just query the view with your key and retrieve all documents
without that field.
Hope this helps.
Cheers,