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).
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 (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:
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:
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With