Akshat Maltare
Akshat Maltare

Reputation: 33

Creating a search query in cloudant using node.js functions

How can I create a function in Node.js that could perform a query in my cloudant database and return me the matching documents which contain a particular key having a particular value in it. For example lets say following is by document:-

{
  "_id": "23966717-5A6F-E581-AF79-BB55D6BBB613",
  "_rev": "1-96daf2e7c7c0c277d0a63c49b57919bc",
  "doc_name": "Markdown Reference",
  "body": "Lorem Ipsum",
  "ts": 1422358827
}

and I want to search this document with its value in ts field how can I write a function (syntax) in my app.js file which will take ts value as input and return me all the documents that contain similar ts value.

Upvotes: 0

Views: 4416

Answers (2)

Isaac Obella
Isaac Obella

Reputation: 2643

I was faced with a similar problem so i used the official cloudant node module.

After installing it using $ npm install --save cloudant ,I wrote a simple module to query my cloudant database.

cloudant.js

//Query
exports.couchQuery = function(query){
    var Cloudant=require('cloudant');
  var cloudant = Cloudant({url: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', plugin:'promises'});
    var mydb = cloudant.db.use('xxxxxxxxxx');

    return mydb.find(query).then(res=>{
        return res;
    }).catch(err=>{
        return err;
    });
}

I used the plugin:'promises' coz i prefer to use promises over callbacks.

routes.js

app.get("/query", function (req, res) {
        couchdb.couchQuery({
            "selector": {
                "type": "input",
                "_id": {
                    "$gt": 0
                }
            },
            "fields": [
                "name",
                "quantity"
            ],
            "sort": [
                {
                    "_id": "asc"
                }
            ]
        }).then(result => {
            if (result.docs) {
                res.send(result.docs);
            }
        })
    });

This worked for me.

Upvotes: 0

Raj
Raj

Reputation: 544

  1. create a Cloudant Query index on your database like this:

    curl -u $username:$password -X POST https://$username.cloudant.com/$databasename/_index -H "Content-Type:application/json" -d '{"index": {"fields": ["ts"]}}'
    
  2. make your database publicly readable

  3. Here's the Node.js function:

    #!/usr/bin/env node
    var request = require('request');
    var ts = process.argv[2];
    if (!ts) {
      console.log('Usage: app.js <ts>');
      process.exit(1);
    }
    query(ts);
    
    function query(tsval) {
      var cloudantquery = {
            "selector": {
              "ts": {"$eq": parseInt(tsval)}
            },
            "fields": ["_id","doc_name"]
      };
    
      request({
            method: 'POST', 
            uri: 'https://$username.cloudant.com/$databasename/_find', 
            json: true, 
            body: cloudantquery
      }, function (error, response, body) {
            if (!error && response.statusCode == 200) {
              console.log(body);
            }
      });
    }
    

Upvotes: 1

Related Questions