How to get a list of keys / documents in couchbase database in C #

I am completely new to couchbase.

This is an example of the code that I use to insert and receive documents:

using (var bucket = Cluster.OpenBucket()) { var document = new Document<dynamic> { Id = "Hello", Content = new { name = "Couchbase" } }; var upsert = bucket.Upsert(document); if (upsert.Success) { var get = bucket.GetDocument<dynamic>(document.Id); document = get.Document; var msg = string.Format("{0} {1}!", document.Id, document.Content.name); Console.WriteLine(msg); } Console.Read(); } 

But I do not know how to get a list of saved documents.

+5
source share
5 answers

So far, Couchbase has two different ways to query the contents of a document: using Views or using the N1QL query language (named nickel and in preview state 3 for developers right now).

Views

Couchbase Views creates indexes based on the contents of JSON documents stored in a database and written using the MapReduce programming model . Couchbase uses MapReduce to process documents in a cluster and to create indexes based on their contents. A view is a JavaScript function that runs for each element in a dataset, performs some initial processing and filtering, and then displays the converted result as a key-value set.

The following image shows what the structure looks like:

View structure

For example, suppose you have a basket named test and you want to store documents with the following structure:

 public class User { [JsonProperty("user_id")] public string UserId { get; set; } [JsonProperty("fname")] public string FirstName { get; set; } [JsonProperty("age")] public string Age { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("type")] public string Type { get; set; } } 

Now suppose you want to find all users who are 25 years old and you want to know their name and email address. Your opinion may be as follows:

 function(doc, meta) { if (doc.type == "user" && doc.age == 25) { emit(doc.user_id, [doc.fname, doc.email]); } } 

If you save this view as a development view with the project document name = dev_user and view name = userswith25, you can use this view in your code as follows:

 var query = bucket.CreateQuery("dev_user", "userswith25"); var result = bucket.Query<dynamic>(query); 

If you want to know more about views, watch this video: Views and Indexing for Couchbase 3.0

N1QL

N1QL (pronounced nickel) is the next generation Couchbases query language. N1QL aims to meet the needs of queries of distributed document-oriented databases. A simple query in N1QL consists of three parts:

  • SELECT - parts of the document to return
  • FROM - a data basket or data warehouse for working with
  • WHERE - Conditions to be met by the document

The query requires only a SELECT . The wildcard * denotes all parts of the document. Queries can return a collection of different structures or fragments of a document. However, they will all meet the conditions in the WHERE clause.

As I said, N1QL is in a preview state, so it is not yet integrated with Couchbase. [EDIT. Integration .NET SDK N1QL is no longer in the alpha version. ] To play with it, you need to download it and integrate with your Couchbase server. Following the previous viewing example, I will show you a query to search for users with the same conditions:

 var query = "SELECT fname, email FROM test WHERE type = 'user' and age = 25"; var result = bucket.Query<dynamic>(query); 

In parallel with the development of N1QL, Coushbase is developing a Language Integrated Query (LINQ) provider for querying a Couchbase server with N1QL using the Couchbase.NET SDK. This will bring the familiar LINQ syntax for N1QL, and the results will be mapped to POCO. Below I will show an example of how you could use it in the future:

 using (var cluster = new Cluster()) { using (var bucket = cluster.OpenBucket("test")) { var users = from c in bucket.Queryable<User>() where c.Age==25 select c; foreach (var user in users) { Console.WriteLine("\tName={0}, Age={1}, Email={2}", user.FirstName, user.Age, user.Email ); } } } 

There are also other options:

  • Telerik created the Linq provider for Couchbase , which I have not used yet, but I think that it is based on the Couchbase.NET SDK 1.3, and not on the version 2.0 that you are using.
  • You can integrate Couchbase with Elasticsearch to provide full-text search in your application using the open source Elasticsearch search engine. With this combination, you can save your documents in Couchbase and search for them later using Elasticsearch. For this you can use this client .sesesesearch.net

Hope this helps.

+16
source

I agree with Simon. Views to find keys or N1QL. If you have not installed or interacted with N1QL yet, this is a very powerful language, and here you can find a tutorial:

http://docs.couchbase.com/developer/n1ql-dp3/n1ql-intro.html

+2
source

To find keys matching the criteria, there are views (and N1QL), as shown in other answers.

Another use case is having a list of keys to retrieve the relevant documents without using a loop:

In the bucket, you can use the IDictionary<string, IOperationResult<T>> Get<T>(IList<string> keys) method. It takes a list of keys that you want to receive, and returns one IOperationResult per key in the dictionary, each of which you can use as you used the get variable in your example.

TPL is used under the explorer, so you do not need to work with parallelism, but you can provide parallelism parameters in overrides.

Here is some documentation of bulk operations with the 2.0.NET SDK. Should be relevant, even if this is documentation for BETA.

Note. Using views usually gives you the identifier / content of the index, but also the document associated with it, so when using views, you donโ€™t even need to do bulk access.

+1
source

Note that N1QL is a query language and must work with views in order to scan and retrieve all keys from the couchbase server. N1QL allows you to create a primary index in your dataset (using views), and then use that index to run your queries.

After installing and configuring N1QL, the following sample query will create an index index / primary index on a couchbase server

Create a primary index on the bucket;

After creating the index, you can get all the keys from the server.

* select * from the bucket;

Internally, the query engine will use the index / primary index to retrieve the server key list (full scan to bucket), and then use this list to retrieve the values.

0
source

N1QL Developer Preview must use views. Your index creation syntax is accurate, but Couchbase 4.0 also introduces secondary indexes and the ability to dynamically scale queries and indexes. When this happens, called "multidimensional scaling," N1QL should by default use the new indexes as the primary indexing scheme and much faster than the views.

Read more about multidimensional scaling here:

http://www.couchbase.com/coming-in-couchbase-server-4-0

0
source

Source: https://habr.com/ru/post/1442034/


All Articles