3

Is there a way to read all the documents from a bucket? It is an active bucket an I want to access newly created document as well.
Few people suggested to use to view to query against a bucket.
How can I create a View which will be updated with new or updated documents?
Newly created view's map function:

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

Reduce function is empty. When I query the view like this bucket.query(ViewQuery.from("test1", "all")).totalRows() it returns 0 results back.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291

2 Answers2

5

For returning zero results issue, did you promote the view to a production view? This is a common mistake. Development views only look at a small subset of data so as to not possibly overwhelm the server. Try this first.

Also, never emit the entire document if you can help it, especially if you are looking over all documents in a bucket. You want to emit the IDs of the documents and then if you need to get the content of those objects, do a get operation or bulk operation. I would give you a direct link for the bulk operations, but you have not said what SDK you are using and those are SDK specific. Here is the one for Java, for example.

All that being said, I have questions about why you are doing the equivalent of select * from bucket. What are you planning to do with this data once you have? What are you really trying to do? There are lots of options on how to solve this of course.

NoSQLKnowHow
  • 4,449
  • 23
  • 35
  • +1 for the tip regarding never emitting full docs. the motivation is this: by emitting a full doc you're creating a duplicate of the data in the index. In other words, for every document you're creating another, identical doc in the index. not good. – FuzzyAmi Nov 17 '15 at 18:59
  • One other thing I might recommend is to take a look at this blog post. http://blog.couchbase.com/2015/october/determine-data-access-in-couchbase – NoSQLKnowHow Nov 18 '15 at 23:16
2

A view is just a predefined query over a bucket. New or changed documents will be shown in the view.

You can check the results of your View when you create it by clicking the Show Results button in the Web UI, so if 0 documents show up there, it should be no surprise you get 0 from the SDK.

If you are running Couchbase Server 4+ and the latest SDK, you could use N1QL and create a primary index on your bucket, then do a regular Select * from bucket to get all the documents.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • How can I get all the documents from a bucket without a view? – Himanshu Yadav Nov 17 '15 at 13:53
  • Aren't you confusing couchbase with couchdb in your answer @cricket_007? In couchbase you do need a view and you shouldn't emit the doc. The Java sdk will very easily allow you to retrieve the full doc at query time, see kirk's answer – Simon Baslé Nov 18 '15 at 14:43
  • @SimonBaslé - Nah, I was thinking about Couchbase, I was trying to figure it out yesterday because I had code, but it actually did use a view :\ And just because you shouldn't emit the doc, doesn't mean you can't :) Alternatively, N1QL doesn't use Views – OneCricketeer Nov 18 '15 at 15:37
  • 1
    `n1ql` is indeed an alternative that is worth mentioning and pushing, if Couchbase Server 4.0 is on the table – Simon Baslé Nov 18 '15 at 17:37