query.keys with empty dictionary

25 views
Skip to first unread message

Aj

unread,
Sep 22, 2015, 7:02:23 AM9/22/15
to Couchbase Mobile


I've written a map function that has

emit([doc.university,doc.degree,doc.studentName],1)

Now, I'd like to query the exact rows (not a key range) as following

query.keys = [[university1,degree1,{}],[university2,degree2,{}]]

I'm fetching all the students (sorted based on their name) that belong to specific degrees and universities. I received 0 results when I tried. 

Jens Alfke

unread,
Sep 22, 2015, 11:41:56 AM9/22/15
to mobile-c...@googlegroups.com

> On Sep 22, 2015, at 4:02 AM, Aj <ajith...@icloud.com> wrote:
>
> Now, I'd like to query the exact rows (not a key range) as following
>
> query.keys = [[university1,degree1,{}],[university2,degree2,{}]]
>
> I'm fetching all the students (sorted based on their name) that belong to specific degrees and universities. I received 0 results when I tried.

That’s because the keys you’re giving it aren’t ones that are in the index — their 2nd element is an empty dictionary, not a student name. (Empty dictionaries aren’t magic wildcards; they’re just values that sort after all other data types, which makes them useful for specifying the upper end of a key range.)

If you want all the rows for a specific university, you do need to use a key range:
startKey = [university]
endKey = [university, {}]

Or in 1.1 and later, you can use:
startKey = endKey = [university]
prefixMatchLevel = 1

—Jens

Aj

unread,
Sep 22, 2015, 1:26:29 PM9/22/15
to Couchbase Mobile

If you want all the rows for a specific university, you do need to use a key range:
        startKey = [university]
        endKey = [university, {}]

And, If I want more than one specific university rows ? for example,  get all the rows for university names, "MIT" & "VTU".

Let me explain again.

emit([doc.university,doc.degree,doc.studentName],1)
 
I want all the rows sorted based on 'studentName' where university is "MIT" and degrees are "Bachelor" & "Master". 

I know that query.keys = [ ["MIT","Bachelor","Aj"], ["MIT", "Master", "AJ"] ] would gimme matching results.  But, I don't know student names. So, I tried putting an empty dictionary to indicate that I want all the studentName to be included. 

That doesn't seems to be working. Can't this be made to work? 

I know that I can write a new view as emit([doc.university,doc.degree],studentName)  and perform sorting in-memory or using sortDescriptors.  But I'm trying to find a solution by avoiding this. 

Jens Alfke

unread,
Sep 22, 2015, 1:47:59 PM9/22/15
to mobile-c...@googlegroups.com
On Sep 22, 2015, at 10:26 AM, Aj <ajith...@icloud.com> wrote:

And, If I want more than one specific university rows ? for example,  get all the rows for university names, "MIT" & "VTU”.

You’ll need to run multiple queries, because those are multiple disjoint key ranges in the index.

I know that query.keys = [ ["MIT","Bachelor","Aj"], ["MIT", "Master", "AJ"] ] would gimme matching results.  But, I don't know student names. So, I tried putting an empty dictionary to indicate that I want all the studentName to be included. 
That doesn't seems to be working. Can't this be made to work? 

As I said before, an empty dictionary is not a wildcard or placeholder. It’s simply a value that sorts after all other values (due to the ordering defined by JSON collation), so it’s convenient to use as an end marker in a key range. It doesn’t make any sense to use an empty dictionary in a “keys” property.

—Jens
Reply all
Reply to author
Forward
0 new messages