Skip to content

Problem with the Views(Map/Reduce) example #10

@7stud

Description

@7stud

Couchbase: Community Edition 5.0.1 build 5003

I'm having problems with the Views example on the README page. When I do:

res.each do |row|
  # Returns extended results by default
  puts "#{row.key}: #{row.value}"
end

The output I get is:

hello-world: {:title=>"Hello World", :body=>"Well hello and welcome to my new blog...", :date=>"2009/01/15 15:52:20"}
biking: {:title=>"Biking", :body=>"My biggest hobby is mountainbiking. The other day...", :date=>"2009/01/30 18:04:11"}
bought-a-cat: {:title=>"Bought a Cat", :body=>"I went to the the pet store.", :date=>"2009/01/30 20:04:11"}

I expected the output:

"2009/01/15 15:52:20": "Hello World"
"2009/01/30 18:04:11": "Biking"  
"2009/01/30 20:04:11": "Bought a Cat"

Because the map function is this:

     "map": "function(doc){if(doc.date && doc.title){emit(doc.date, doc.title);}}"

I think map() should emit the date as row.key and the title as row.value.

Full code, which is copied from the README:

require 'libcouchbase'

bucket = Libcouchbase::Bucket.new(
  hosts: '127.0.0.1', 
  bucket: 'recent-posts', 
  username: '7stud',
  password: 'abc123')


bucket['biking'] = {:title => 'Biking',
                   :body => 'My biggest hobby is mountainbiking. The other day...',
                   :date => '2009/01/30 18:04:11'}
bucket['bought-a-cat'] = {:title => 'Bought a Cat',
                          :body => 'I went to the the pet store.',
                          :date => '2009/01/30 20:04:11'}
bucket['hello-world'] = {:title => 'Hello World',
                        :body => 'Well hello and welcome to my new blog...',
                        :date => '2009/01/15 15:52:20'}

view_doc = %q|
    {
      "_id": "_design/blog",
      "language": "javascript",
      "views": {
        "recent_posts": {
          "map": "function(doc){if(doc.date && doc.title){emit(doc.date, doc.title);}}"
        }
      }
    }
|

bucket.save_design_doc(view_doc)

blog = bucket.design_docs['blog']
p blog.views    

res = blog.view('recent_posts')  #=> #<Libcouchbase::Results:0x007fbaed12c988>

res.each do |row|
  # Returns extended results by default
  puts "#{row.key}: #{row.value}"
end

I'm not sure what the comment #Returns extended results by default means. If I do:

res = blog.view('recent_posts', include_docs: false)

then my results are:

hello-world: 
biking: 
bought-a-cat: 

It doesn't seem to matter what the map function emit()'s because the results are always the same. For instance, I get the same output with emit(1, 2), emit('abc', '123'), and emit(doc.date, doc.title)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions