Can i have samples for Map reduce views in Couchbase Lite

I am exploring the Map Reduce views for couchbase Lite in C# .

Can i get some samples , examples of how map reduce view and Queries can be used in C# for Couchbase Lite

Hi @vijaykumar.menedhal,

You can check out the View guide to learn how to write views (i.e the logic to create an index persisted to the database) and then check out the Query guide to run a query on a view. The examples are in C# as well.

James

Thanks jamiltz , i have gone through the links suggested . but i am still not clear on them . so wanted some other examples , where i shave sample JSON files and code in C# by which i can create views and run queries.

Any other example will help

I’m pretty sure we have other .NET demos, but here are two I quickly found:


Unfortunately our organization is full of half finished .NET stuff. The second repo listed there is an example of that, and I’ve never seen the first one (it was written 2 years ago). There are some samples right in the .NET repo itself in the samples directory, and also you can find something that was recently used here:

This will go step by step with setting things up

Thanks everyone for you reply . I have started to write a Map function for a specific example . Please guide me if i am write . i am using Couchbase lite in C# .

Below is a simple JSON File :
{
“id”: “0001”,
“type”: “donut”,
“name”: “Cake”,
“ppu”: 0.55,
“batters”:
{
“batter”:
[
{ “id”: “1001”, “type”: “Regular” },
{ “id”: “1002”, “type”: “Chocolate” },
{ “id”: “1003”, “type”: “Blueberry” },
{ “id”: “1004”, “type”: “Devil’s Food” }
]
},
“topping”:
[
{ “id”: “5001”, “type”: “None” },
{ “id”: “5002”, “type”: “Glazed” },
{ “id”: “5005”, “type”: “Sugar” },
{ “id”: “5007”, “type”: “Powdered Sugar” },
{ “id”: “5006”, “type”: “Chocolate with Sprinkles” },
{ “id”: “5003”, “type”: “Chocolate” },
{ “id”: “5004”, “type”: “Maple” }
]
}

i have added this JSON document onto the couchbase lite . now i am trying fetch this document using the document id “kiran589” . and looking to create a map function where my condition would be if the type is “donut” then i want to emit name and Value pair . below is the code snippet i have written :

       var retDocID = db.GetDocument("kiran589");

        var props1 = new Dictionary<string, object>(retDocID.Properties);
        // Create a view and register its map function:
        var view = GetView("batters");

        view.SetMap((props, emit) =>
        {
            if (props1["type"] == "donut")
            {
                emit(props1["name"], props1["value"]);

            }
        },  "2");

is the above map function correct ?

Your document doesn’t have a value property, so emitting props1["value"] isn’t going to do anything.

Also, I think you mean props instead of props1. Haven’t you tried running this?

One immediate thing that is a red flag is that you are not writing a pure function for mapping. You need to use ONLY the properties passed in through the props portion of (props, emit). Maps are constructs that run across the entire database, not only on particular documents. That means that when you query a view it will read from all the documents in the database that it has not read from yet and run them through the function inside of SetMap.

emitting props1[“value”] isn’t going to do anything.

Actually, it will throw an exception which will cancel the update and log that the map function threw an exception.

Hi All ,
Thanks for your reply , first of all sorry for putting the wrong code . below is the code i am using now .

        //retrieve the document from the database
        var retDocID = db.GetDocument("kiran589");

        var props = new Dictionary<string, object>(retDocID.Properties);
        // Create a view and register its map function:
        var view = GetView("batters");

        view.SetMap((props, emit) =>
        {
            if (props["type"] == "donut")
            {
                emit(props["name"], props["ppu"]);

            }
        },  "2");

First of all :

  1. when i use the term props in the view.SetMap Function i get a compilation error as below :slight_smile:

Severity Code Description Project File Line Suppression State
Error CS0136 A local or parameter named ‘props’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

  1. And when i do the comparision check that if props[“type”] ==“donut” i get a compiler warning as below

Severity Code Description Project File Line Suppression State
Warning CS0252 Possible unintended reference comparison; to get a value comparison, cast the left hand side to type ‘string’

  1. what will be the correct way of creating a view (map function) ? and how can i check if the view(Map Function) was properly created

Error 1 happens because you have two declarations of “props”, the var props dictionary and the argument to your lambda function in SetMap. Both these labels are arbitrary. Is there some reason you’re using “props” for both?

Error 2 happens because props[“type”] is not of type string. The compiler is trying to prevent you from mistakenly comparing a string reference to another reference.

The combination of these errors makes me think there’s a misunderstanding about what’s happening in the SetMap function. You’re handing your SetMap a lambda expression. I think this SO question has some good explanations and pointers to other resources that might help: http://stackoverflow.com/questions/10587909/lambda-scope-clarification

Hod

SO what would be the correct code snippet which will create a view function which has values of name and PPu ,

If the type is doNut

This code doesn’t make sense; you’re mixing up what happens when the view is indexed (the map function) with what happens at query time (getting a specific document.)

All you need is:

        // Create a view and register its map function:
        var view = GetView("batters");

        view.SetMap((props, emit) =>
        {
            if (props["type"] == "donut")
            {
                emit(props["name"], props["ppu"]);

            }
        },  "2");

You don’t need (and shouldn’t have) any declaration of props outside the lambda. That variable is passed to the lambda by the indexer, corresponding to the document being indexed. The map function’s job is to convert that doc into a key and value to be emitted into the index.