如何在C#中检索Couchbase数据库中的键/文档列表 [英] How to retrieve a list of keys/documents in couchbase database in C#

查看:81
本文介绍了如何在C#中检索Couchbase数据库中的键/文档列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的沙发床。



这是我用于插入和获取文档的示例代码:

 使用(var bucket = Cluster.OpenBucket())
{
var document = new Document< dynamic>
{
Id = Hello,
内容=新的
{
名称= 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();
}

但是我不知道如何检索已存储文档的列表。

解决方案

直到现在,在Couchbase中,有两种不同的查询文档内容的方法:使用


N1QL


N1QL(发音为镍 )是Couchbase的下一代查询语言。 N1QL旨在满足面向分布式文档的数据库的查询需求。 N1QL中的一个简单查询包含三个部分:



  • SELECT -要返回的文档部分

  • FROM -要使用的数据存储区或数据存储区

  • 位置-文档必须满足的条件


查询中只需要 SELECT 子句。通配符 * 选择文档的所有部分。查询可以返回不同文档结构或片段的集合。但是,它们都将符合WHERE子句中的条件。


正如我之前所说,N1QL处于开发人员预览状态,因此尚未与Couchbase集成。

s> 要使用它,您需要下载它并将其与Couchbase服务器集成。在前面的视图示例之后,我向您展示了一个查询条件相同的用户:

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

与N1QL的开发并行,Coushbase正在开发语言集成查询(LINQ)提供程序,用于使用Couchbase .NET SDK通过N1QL查询Couchbase服务器。这将为N1QL带来熟悉的LINQ语法,并将结果映射到POCO。下面我显示了一个示例,说明了将来如何使用它:

 使用(var cluster = new Cluster())
{
使用(var bucket = cluster.OpenBucket( test)))
{
var users =来自bucket中的c。Queryable< User>()
其中c.Age == 25
选择c;

foreach(用户中的用户)
{
Console.WriteLine(&tName = {0},Age = {1},Email = {2} ;,
user.FirstName,
user.Age,
user.Email
);
}
}
}

此外,还有其他变体:



希望这会有所帮助。


I'm totally new to couchbase.

This is the sample code I use for insert and get 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 have no idea how to retrieve a list of stored documents.

解决方案

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:

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.

这篇关于如何在C#中检索Couchbase数据库中的键/文档列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆