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

后端 未结 5 1216
说谎
说谎 2021-01-01 02:01

I\'m totally new to couchbase.

This is the sample code I use for insert and get documents:

using (var bucket = Cluster.OpenBucket())
{
    var docume         


        
相关标签:
5条回答
  • 2021-01-01 02:33

    I concur with Simon as well. Views to find the keys or N1QL. If you have not yet setup or interacted with N1QL, it is a very powerful language and a tutorial can be found here:

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

    0 讨论(0)
  • 2021-01-01 02:37

    To find keys that meet a criteria, there's views (and N1QL upcoming) as seen in others responses.

    The other use case is, having a list of keys, to retrieve the corresponding documents without using a loop:

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

    Under the wire, this uses the TPL so there's no need for you to work on parallelism, but you can provide parallelism settings in overrides.

    Here is some documentation on bulk operations with the 2.0 .Net SDK. Should be relevant even though this is documentation for the BETA.

    Note: the usage of views generally gives you the id / content of the index, but also the associated document, so when using views you shouldn't even have to do a bulk get.

    0 讨论(0)
  • 2021-01-01 02:37

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

    After you install and setup N1QL the following example query will create a view index/primary index on the couchbase server

    Create primary index on bucket;

    Once the index is created then you can retrieve all the keys from the server.

    *select * from bucket;

    Internally the query engine will use the view/primary index to fetch a list of keys of the server (full bucket scan) and then use that list to retrieve the values.

    0 讨论(0)
  • 2021-01-01 02:41

    N1QL Developer previews must use views. Your syntax of create index is accurate, but Couchbase 4.0 is also introducing secondary indexes and the ability to scale query and index capabilities dynamically. When this happens, called "Multi-Dimensional Scaling", N1QL should default to the new indexes as the primary indexing scheme, and much much faster than views.

    More can be read about multi-dimensional scaling here:

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

    0 讨论(0)
  • 2021-01-01 02:50

    Until now, In Couchbase there are two different ways to query document content:using Views or using N1QL query language (named nickel and in developer preview 3 state right now).

    Views

    Couchbase Views creates indexes based on the content of JSON documents stored in the database and are written using MapReduce programming model. Couchbase uses MapReduce to process documents across the cluster and to create indexes based on their content. A view is a JavaScript function which is executed on every item in the dataset, does some initial processing and filtering, and then outputs the transformed result as a key-value set.

    The following image show you what is the structure view:

    View Structure

    For example, suppose that you have a bucket called 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 that you want to find all the users who are 25 years old and you want to know their name and email. Your view could be at this way:

    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 Design Document Name= dev_user and View Name=userswith25, you could use this view in your code this way:

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

    If you want to learn more about views, take a look this video:Views and Indexing for Couchbase 3.0

    N1QL

    N1QL (pronounced “nickel”) is Couchbase’s next-generation query language. N1QL aims to meet the query needs of distributed document-oriented databases. A simple query in N1QL has three parts to it:

    • SELECT - Parts of document to return
    • FROM - The data bucket, or datastore to work with
    • WHERE - Conditions the document must satisfy

    Only a SELECT clause is required in a query. The wildcard * selects all parts of the document. Queries can return a collection of different document structures or fragments. However, they will all match the conditions in the WHERE clause.

    As I said before, N1QL is in developer preview state, so isn't integrated with Couchbase yet. [EDIT: The .NET SDK N1QL integration no longer appears to be in alpha. ] To play with it you need to download it and integrate it with your Couchbase server. Following the previous view example, I show you a query to search users with the same conditions:

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

    Parallel to the development of N1QL, Coushbase is developing a Language Integrated Query (LINQ) provider for querying Couchbase Server with N1QL using the Couchbase .NET SDK. This will bring familiar LINQ syntax to N1QL and the results will be mapped to POCOs. Below I 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
                            );
                    }
                }
            }
    

    Also, there are other variants:

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

    Hope this helps.

    0 讨论(0)
提交回复
热议问题