Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb mapreduce function does not provide skip functionality, is their any solution to this?

Tags:

mongodb

nosql

Mongodb mapreduce function does not provide any way to skip record from database like find function. It has functionality of query, sort & limit options. But I want to skip some records from the database, and I am not getting any way to it. please provide solutions.

Thanks in advance.

like image 371
Onkar Janwa Avatar asked Sep 09 '26 19:09

Onkar Janwa


1 Answers

Ideally a well-structured map-reduce query would allow you to skip particular documents in your collection.

Alternatively, as Sergio points out, you can simply not emit particular documents in map(). Using scope to define a global counter variable is one way to restrict emit to a specified range of documents. As an example, to skip the first 20 docs that are sorted by ObjectID (and thus, sorted by insertion time):

db.collection_name.mapReduce(map, reduce, {out: example_output, sort: {id:-1}, scope: "var counter=0")}; 

Map function:

function(){
    counter ++;
    if (counter > 20){
        emit(key, value);
    }
}
like image 140
Jenna Avatar answered Sep 11 '26 09:09

Jenna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!