emit(nil, nil) for _view/_all

25 views
Skip to first unread message

dyowee

unread,
Apr 4, 2017, 9:45:57 PM4/4/17
to Couchbase Mobile
Good day,

I updated from iOS version 1.2 to 1.4. But it seems that the some views are not working anymore, specifically those with emit(nil, nil). I use emit(nil, nil) if I need to get all documents of the same type. Are there any changes I need to make to the setMapBlock call?

Thanks,
Joey

Jens Alfke

unread,
Apr 4, 2017, 11:30:50 PM4/4/17
to mobile-c...@googlegroups.com

> On Apr 4, 2017, at 6:45 PM, dyowee <csharpen...@gmail.com> wrote:
>
> Good day,
>
> I updated from iOS version 1.2 to 1.4. But it seems that the some views are not working anymore, specifically those with emit(nil, nil). I use emit(nil, nil) if I need to get all documents of the same type.

nil is not a valid key in the index, so emitting it is an error. It may be that CBL used to ignore it, but it will now throw an exception.
What is it you’re trying to do?

—Jens

dyowee

unread,
Apr 4, 2017, 11:52:46 PM4/4/17
to Couchbase Mobile
Thank you for your response. I am trying to get all documents of the same type (field with the same value, e.g., doc.type). 

dyowee

unread,
Apr 5, 2017, 12:27:13 AM4/5/17
to Couchbase Mobile
Like what is done in CouchDB to return all documents of the same type

"all": {

   "map": "function(doc) { if (doc.type == 'customer')  emit(null, doc) }"

}


On Tuesday, April 4, 2017 at 8:30:50 PM UTC-7, Jens Alfke wrote:

Jens Alfke

unread,
Apr 6, 2017, 3:09:28 PM4/6/17
to mobile-c...@googlegroups.com

On Apr 4, 2017, at 9:27 PM, dyowee <csharpen...@gmail.com> wrote:

Like what is done in CouchDB to return all documents of the same type
"all": {
   "map": "function(doc) { if (doc.type == 'customer')  emit(null, doc) }"
}

That key is a JavaScript `null` object, which is equivalent to a JSON `null`. In Objective-C you would express that as an NSNull object, i.e. `[NSNull null]`.
JavaScript is weird: its `null` is a real value. It's not the same thing as an Objective-C `nil` (or a Java or C# `null`, or a C `NULL`) — those are all equivalent to JavaScript’s `undefined`.

In this map function the `null` is just being used as a placeholder because the developer doesn’t care what order the values should be in, so it gives them all the same key. It actually doesn’t matter what key you use here; you could put `0` or an empty string, or even 12345 or “foobar”.

If you find yourself creating multiple views like this but with different document type strings, a better way to do it is to emit doc.type as the key, and emit it for all docs that have a `type` property. This gives you a single view that can be used to find docs of all types, not just ‘customer’.

—Jens

dyowee

unread,
Apr 8, 2017, 5:38:07 PM4/8/17
to Couchbase Mobile
Thank you very much. Will try.
Reply all
Reply to author
Forward
0 new messages