Search code examples
databasecloudantnosql

can i query Cloudant NOSQL data base for specific data?


"data": { "d": [ 100, 0, 1, 1, 0, 22, 55, 66, 99, 87 ] this is my data format on Cloudant NOSql Database each integer inside array "d" corresponds to specific parameter values from device, is it possible to query only the required data(may be 100) which correspond to specific parameter, from the array?


Solution

  • Create a view with this map function, which returns all documents that have the value 100 in the first index of the array data.d:

    function (doc) {
      if (doc.data && doc.data.d && doc.data.d.length>0 && doc.data.d[0] == 100)
        emit([doc._id,doc.data.d[0]], 1);
    }
    

    Here's a query listing a sample database containing 3 data rows and 1 design document containing the above map function:

    https://rajsingh.cloudant.com/aaa/_all_docs?include_docs=true
    

    And here's the query that returns only the documents meeting the view constraints:

    https://rajsingh.cloudant.com/aaa/_design/views/_view/dzero?limit=200&reduce=false&include_docs=true