-1

I need help with a function nano.request(). I try get data by a request using a cloudant (couchdb) query but i no have idea how make it and i search anywhere. please i need help :P how search by query in a nano.request function??

thanks

var query = {
"selector": {
  "_id": {
    "$gt": 0
  },
  "Campaign_Id":9999
},
"fields": [
],
"sort": [
  {
    "_id": "asc"
  }
]};

cloudant.request({db: 'campaigns',
              method: 'get',
              doc: '_all_docs',
              qr: query 
            },function (err,data){
console.log(err);
console.log(data);
});
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • What have you tried? This [db.get](https://github.com/dscape/nano#dbgetdocname-params-callback) might be the call you're looking for. – Rho Jul 22 '16 at 02:34
  • i need make a query to filter for example "selector": { "_id": { "$gt": 0 }, "Campaign_Id":9999 }, "fields": [ ], "sort": [ { "_id": "asc" } – Kevin Morleo Jul 22 '16 at 03:10
  • So you can use [Cloudant Query](https://github.com/cloudant/nodejs-cloudant#cloudant-query) for what you're trying to do. Keep in mind you need to create an index for this first. [Here's](https://cloudant.com/using-cloudant-query-tutorial/) a direct example. – Rho Jul 24 '16 at 19:39
  • However, my experiences with querying in cloudant are related to Cloudant Search instead. When I needed to mix the query to filter on more than one field (i.e. x > 8 OR y < 70) I found that cloudant query didn't support the functionality at the time. – Rho Jul 24 '16 at 19:41

3 Answers3

2
var testRequest = function(query){

cloudant.request({db: 'campaigns',
                method: 'POST',
                doc: '_find',
                body: query
                },function (err,data){
                console.log(data);
});


 }



  var peticion = {
  "selector": {
     "crazy": true,
    "_id": {
      "$gt": 0
    }
  },
  "fields": [
    "_id",
    "_rev"
  ],
  "sort": [
    {
      "_id": "asc"
    }
  ]
}

  testRequest(peticion);
1

This is not exactly an answer to your question, rather just a suggestion. Why don't you try using Search Indexes for your queries? I found they are really brilliant. I've moved from views and queries to search indexes.

Kaushik Evani
  • 1,154
  • 9
  • 17
0

Your query looks good, but like Rho said, you need to build an index on the fields before you can use Cloudant Query. By default, you have the primary index in _id.

You can build indexes in the Cloudant dashboard. Here's what mine looks like: building a Cloudant Query index in the Cloudant dashboard

Here's the syntax to build that index:

{
  "index": {
    "fields": [
      "Campaign_Id"
    ]
  },
  "type": "json"
}

The idea is for it to be a little simpler than defining JavaScript MapReduce views. Cloudant Query can also do ad hoc queries once you build a "type": "text" index, but that index is more expensive. More on that at https://cloudant.com/blog/cloudant-query-grows-up-to-handle-ad-hoc-queries/

Community
  • 1
  • 1
brobes
  • 706
  • 3
  • 7