Why emit(meta.id, NULL)
Asked Answered
B

1

6

I've read some sample code (especially from the Couchbase Model Views demo project link) and realized the map() function is so strange.

function(doc, meta) {
   if (doc.type == "beer" && doc.name){
       emit(doc.name, null);
   }
}

Why the emit function's value is null, but the result from GetView("beers", "beer") is getting the value perfectly???

Please help me out !

Biting answered 23/8, 2013 at 4:20 Comment(1)
actually because of javascript nature, you can call just emit(doc.name)Weinhardt
S
8

In couchbase usally view's result set is built in background. If you have for example 1 million documents each 4Kb size without any views it take ~4Gb on disk. When you create a view with map function like

function(doc, meta) {
   emit(doc.name, doc);
}

As a result it take additional 4Gb on disk for view results because view results are stored separately. And in most cases (if you query view with param Stale=Ok) couchbase returns result from that "precompiled" set of records, couchbase doesn't scan all docs on each query.

So emmiting null in map functions is used to prevent disk space usage and it also increase speed of indexing process.

Now the second question about couchbase magic when "result from GetView("beers", "beer") is getting the value perfectly". Couchbase get(key) and getMulti(keys) operations are very fast. So when you query view that emits null it returns not only nulls, also it returns document ids. Then you can manually use getMulti for that array of document ids to get doc's value or in some SDKs there is query param called IncludeDocs that will do the same automatically.

Synergetic answered 23/8, 2013 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.