Search code examples
cloudantnosql

How would I create an index for an element that's part of an array in cloudant nosql?


Let's say there's a Document such as the following

{
    _id:1234,
    pages:[
    {"name":"a","content":"a1"},
    {"name":"b","content":"b1"},
    {"name":"c","content":"c1"},
    ]
}

How would I create a Primary Index, in order to query "name" as a field?


Solution

  • You could create a map-reduce view to achieve what you want:

    function(doc) {
        if (doc && doc.pages) {
            doc.pages.forEach(function (row) {
                if (row.name) {
                    emit(row.name, 1);
                    // or if you want to lookup the content part, use
                    // emit(row.name, row.content);
                }
            });
        }
     }