0

In my application i'm using node.js with coucdhDB database.Now i want to get the data by given limit if user mentioned start limit as 10 and end limit as 20 means i want to get the data between 10 to 20.How can i do this.

user1629448
  • 227
  • 1
  • 4
  • 10

1 Answers1

0

Here's an example that should run as is on a public facing CouchDB for NPM data. Pardon the terrible code and poor parameter checking and error handling, but you'll get the idea. Since this example CouchDb view uses text package names I'm not doing your numerical example, instead to get packages with names between "ya" and "yaa" I would access this public facing database with the following:

http://isaacs.iriscouch.com/downloads/_design/app/_view/pkg?group_level=2&start_key=%5B%22ya%22%5D&end_key=%5B%22yaa%22,%7B%7D%5D

The equivalent then against my node server using the code below would be:

http://yourapi.goeshere.xxx:3000/rangetest/ya-yaa

with the following code:

var express = require('express')
  , cradle = require('cradle')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
});

app.get('/rangetest/:from-:to', function(req, res) {
    console.log('entering range test');
    var theFrom = req.params.from;
    var theTo = req.params.to;
    console.log('range sent was ' + theFrom + " to : " + theTo);


    var testConn = new(cradle.Connection)('http://isaacs.iriscouch.com', {
        cache: true,
        raw: false
    });

    var testDb = testConn.database('downloads');
    var testParams = "_design/app/_view/pkg?group_level=2&start_key=%5B%22" + theFrom + 
        "%22%5D&end_key=%5B%22" + theTo + "%22,%7B%7D%5D";
    testDb.get(testParams, function (err, testResult) {
        if (err)
        {
            console.log(err);
            res.json(500, err);
        } else {
            res.json(testResult);
        }
    });

 });
JustTrying
  • 764
  • 6
  • 11