function (doc, meta) {
if (doc.firstName && doc.lastName)
{
emit([doc.firstName, doc.lastName], doc);
}
}
Here's how this query works... the program sends an object array consisting of a first name and a last name to Couchbase. It searches the bucket and server node specified in the server instance for documents with elements called "firstName" and "lastName". If a document qualifies, and the incoming data matches that document's specified contents, that document included in the returned collection.
But, first things first. In this case, I created a console app, and modified the app.config file to include the following:
<section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
</configSections>
<couchbase>
<add uri="http://127.0.0.1:8091/pools"/>
</servers>
</couchbase>
These entries go before any other node in the root node. You might need to change the URI, butcket, or password, given your configuration. You can have multiple URIs as well.
If you change your app.config (or web.config as the case might be), you can create a server instance with just
var client = new CouchbaseClient();
In case you're wondering which libraries I added, here they are...
using Couchbase;
using Couchbase.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Before I go any further, I used the following class for my data...
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
namespace ConsoleCouchbase2
{
public class MyName
{
[JsonProperty("FirstName")]
public string FirstName { get; set; }
[JsonProperty("LastName")]
public string LastName { get; set; }
}
}
Couchbase stored the names of the elements in "camel" format. This caused me some confusion for quite a while.
Anyway, back to the main code. The document Store methods I experimented with were Add and Set. "Add" adds the document if that document ID doesn't exist in the bucket. "Set" adds the document if that ID doesn't exist; if it exists, the old document gets replaced with the new one. Here is how I stored my docnuments...
var test1 = new MyName() { FirstName = "Ross", LastName = "Albertson" };
var test2 = new MyName() { FirstName = "David", LastName = "Albertson" };
client.StoreJson(Enyim.Caching.Memcached.StoreMode.Add, "1", test1);
client.StoreJson(Enyim.Caching.Memcached.StoreMode.Add, "2", test2);
StoreJson is needed to store structured objects. Store can be used for storing strings or other single units of information. The second argument is the document ID.
Now, to put it all together...
I first made a "key" object containing the data I want to match...
string[] key = { "Ross", "Albertson" };
To find a matching document, I did the following...
var view = client.GetView<MyName>("dev_names", "get_name").Key<string[]>(key);
foreach (var oneName in view)
{
Console.WriteLine("Found: {0} {1}", oneName.FirstName, oneName.LastName);
}
"dev_names" is the name of the design document; "get_name" is the name of the view. I told Couchbase to give me a collection of MyName's. The key type is an array of strings. If I wanted all the documents this view was capable of giving me, I would write
var view4 = client.GetView<MyName>("dev_names", "get_name");
foreach (var foundName in view4)
{
Console.WriteLine("{0} {1}", foundName.FirstName, foundName.LastName);
}
If you wanted to create a view for this specific purpose, emit(null, doc); for a function body would work. If instead you wanted to count the matching documents, creating a view with the same "map", but with _count as the reduce (using just that) will do the job. I did just that with the view name "count_matches". This is the code I used to count my matches...
var view2 = client.GetView<int>("dev_names", "count_matches").Key<string[]>(key);
foreach (var x in view2)
{
Console.WriteLine("Found {0} match(es)", x);
}
You can probably guess that
var view3 = client.GetView<int>("dev_names", "count_matches");
gives me total number of possible matches. Lastly, if you want to specify the scope of client, you can use a "using" block. I hope this post helps!
No comments:
Post a Comment