Search code examples
couchdbcouchdb-futon

Json array iteration in futon


I am using CocuhDB I have this document structure

{"master": false,
"type": "a",
"company": 9,
  "products": [
       {
           "unit": {
               "id": 9,
               "isMp": false,
               "_id": "40109daadce8d3671a1aeca35bbb1438"
           },
           "article": {
               "id": 1132,
               "id_provider": 0,
               "isMp": false,
               "id_unit": 9,
               "weight": 0,
               "_id": "eb1718f96375b01af5552cf3f4c2d86b",
               "code": "ME021",
               "type_article": 2
           },
           "order": 1,
           "warehouse_company": {
               "id": 9,
               "_id": "ebce7557ff95203ac5d03f294381d6ed"

           },
           "article_code": "ME021",
           "provider": {
               "id": 2313,
               "isMp": false
           },
           "qty": 20.5,
           "warehouse": {
               "id": 18,
               "isMp": false
           }
       },   {
           "unit": {
               "id": 9,
               "isMp": false,
               "_id": "40109daadce8d3671a1aeca35bbb1438"

           },
           "article": {
               "id": 1132,
               "id_provider": 0,
               "isMp": false,
               "id_unit": 9,
               "weight": 0,
               "_id": "eb1718f96375b01af5552cf3f4c2d86b",
               "code": "ME099",
               "type_article": 2
           },
           "order": 1,
           "warehouse_company": {
               "id": 9,
               "isMp": false
           },
           "article_code": "ME021",
           "provider": {
               "id": 2313,
               "isMp": false,
               "_id": "657abbdfb4c713a9baa1ffd7329319c0"
           },
           "qty": 20.5,
           "warehouse": {
               "id": 18,
               "isMp": false,
               "_id": "9f70abb04a0243a1cd997b6430fb2207"
           }
       }
   ]
}

enter image description here

products field coulb be one or ten. I need to find all the documents where

doc.products[n].warehouse.id == 18

But I dont know how to do it using Futon. I am trying something like :

function(doc) {
    var product, value;
    if (doc.type != master && doc.type == "a" && doc.company == 9) {
        for (product in doc.products) {
            value= doc.prices[producto];
            emit(value, doc );
        }
    }
}

But It does not work .
What I am doing wrong?


Solution

  • You can use view like this. This is an example. Not a solution

    function(doc) {
        var product, value;
        if (doc.type != "master" && doc.type == "a" && doc.company == 9 && doc.products && Array.isArray(doc.products)) {
            doc.products.forEach(function(product) {
              Object.keys(product).forEach(function(key) {
                emit([key, product[key].id]);
              });
            });
        }
    }
    

    Will create output like

    {
        "total_rows": 16,
        "offset": 0,
        "rows": [
            {
                "key": [
                    "article",
                    1132
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "article",
                    1132
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "article_code",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "article_code",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "order",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "order",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "provider",
                    2313
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "provider",
                    2313
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "qty",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "qty",
                    null
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "unit",
                    9
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "unit",
                    9
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "warehouse",
                    18
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "warehouse",
                    18
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "warehouse_company",
                    9
                ],
                "id": "check",
                "value": null
            },
            {
                "key": [
                    "warehouse_company",
                    9
                ],
                "id": "check",
                "value": null
            }
        ]
    }
    

    You can search for " warehouse" with id 18 like

    using key=["warehouse",18]