Filter by existance of property?


(Joel Abrahamsson) #1

Hi!

Seeing how this is my first post here I want to start by thanking all
contributors for a great project!

Now on to my question. I'm wondering if it's possible to filter
documents based on the existence of a JSON property. Let's say I have
two documents:

FIRST:
{
"Title":"Great rock",
"Author":{
"Id":1,
"Name":"Joel"
}
}

SECOND:
{
"Title": "More great rock music"
}

I would like to find only those that have an author. However, both the
Id and Name of the author can be null.
My first instinct was to try:
{
"filter":{
"exists":{
"field":"Author"
}
}
}

However that doesn't work. I'm guessing that's the case because Author
isn't actually a field, only it's child properties are?

I could of course filter on one of the Author properties child-
properties but seeing how they could both be null I would probably
have to do an OR filter on both which seems like a last resort.

Any ideas?

Sorry if this is a question that has been answered before. I've done
my best trying to find discussions about similar problems but with no
luck.

Thanks in advance! /Joel


(Shay Banon) #2

Heya,

Yes, you will need to explicitly OR between the author fields, no way
around it unless you add a special marker boolean field on each author
object, and only ask on it (better for perf).

-shay.banon

On Sat, Jul 16, 2011 at 10:09 PM, Joel Abrahamsson <mail@joelabrahamsson.com

wrote:

Hi!

Seeing how this is my first post here I want to start by thanking all
contributors for a great project!

Now on to my question. I'm wondering if it's possible to filter
documents based on the existence of a JSON property. Let's say I have
two documents:

FIRST:
{
"Title":"Great rock",
"Author":{
"Id":1,
"Name":"Joel"
}
}

SECOND:
{
"Title": "More great rock music"
}

I would like to find only those that have an author. However, both the
Id and Name of the author can be null.
My first instinct was to try:
{
"filter":{
"exists":{
"field":"Author"
}
}
}

However that doesn't work. I'm guessing that's the case because Author
isn't actually a field, only it's child properties are?

I could of course filter on one of the Author properties child-
properties but seeing how they could both be null I would probably
have to do an OR filter on both which seems like a last resort.

Any ideas?

Sorry if this is a question that has been answered before. I've done
my best trying to find discussions about similar problems but with no
luck.

Thanks in advance! /Joel


(system) #3