How to get all documents from the data bucket usin

2019-06-27 13:23发布

问题:

How can I get all the documents from the data bucket?

I have tried a sample but I'm able to get only a specific document. Here is my code:

CouchbaseClient oclient;

oclient= new CouchbaseClient("vwspace", "");// data bucket name

var results = oclient.Get("205");// document id

How to get all the documents?

var results = oclient.? //what should i use here to get all documents

回答1:

Using Couchbase Server 2.0, you would use a view to get all documents. Your view would look like:

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

This view will give you access to all of the ids (id is always included in non-reduced view query results).

For more on views and querying views in .NET, see http://blog.couchbase.com/strongly-typed-views-net-client-library.



回答2:

You cannot get all documents. Create one "constant" atomic integer value that will be counter like this:

CouchbaseClient oclient;

oclient= new CouchbaseClient("vwspace", "");// data bucket name

ulong results = (ulong)oSourceBucket.Get("MYCOUNTER");// counter (integer incremental value)

When you add documents to bucket don't add them with some documentId (I suppose you get that form SQL database or something) but make them with counter like this:

results = oSourceBucket.Increment("MYCOUNTER", results, 1);// counter (integer incremental value)

oSourceBucket.Store(StoreMode.Add, "MYITEM." + results.toString(), myNewObjectToStore);

Now you can just use for loop to oSourceBucket.Get(...) all items up until MYCOUNTER value. I am not sure if new version of Couchbase 2.0 will have pattern gets but current stable version (1.8.1 I think) allows only exact key gets.

Remeber this is KEY-VALUE store, not SQL :)

Also there is MultiGet in Couchbase but in last stable version of .NET client it does not exist but it is used under the hood just like for... loop with multiple Get-s.



回答3:

You'll need to create a couchbase view that emits document id ( meta.id ).

Or use existing view that emits every record.

Then

http://HOST:8092/YOURBUCKETNAME/_design/YOURDESIGNDOCNAME/_view/YOURVIEWNAME?reduce=false&limit=10

You'll get result like the following

{"total_rows":1321085,"rows":[
{"id":"key1","key": ... ,"value": ... },
{"id":"key2","key": ... ,"value": ... },
{"id":"key3","key": ... ,"value": ... },
...
]
}

The "id" field contains document key.

You'll need to paginate over the results. http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-querying-pagination.html