Nodejs Mongodb update callback issue

I am importing a csv file in mongodb using mongodb driver and fast csv.The problem is that for every record i need to insert that record in the collection if that record is not there in the collection and update it otherwise.whether the record exists or no is determined by its id.Apart from this id i also need an auto incrementing _id field.The csv is as shown.

1001,a,201
2001,b,202
2001,c,203
3001,d,204
3001,e,205

fast csv reads and passes to callback as data.

data=[1001,a,201]

so when first record is read it should go in the db as

{ "_id":1,rid:"1001", "a":"101"}

second record :

{ "_id":2, rid:"2001", "b":"102"}

now when third record will be read as record with rid:"2001" already exists,it should just update the record by adding the new key:value in it i.e after update it should be :

{ "_id:2",rid:"2001","a":"101","c":"203"}.

I am using $setOnInsert:{_id:index} in the update argument of the update method.$setOnInsert would not have any effect if the record already exists.Now this combined with setting upsert:true should work fine.But I couldn't figure out how to increment index.I initially tried with using count method , and updating the record in the callback of count but as there is no gurantee about when the callback is executed,after inserting a record, the next time the count for that record still shows as 0 .could this issue be solved using mongoose.In mongoose we need to have a schema.But i want each record to have unknow number of fields like :

{_id:1,rid:"1001","a":"101","c":203","d":"208", .. , .. ,..}

query: fields={} fields[data[1]]=data[2];

db.collection("collectionname").update(query,{$setOnInsert:{_id:++index},$set:fields},{upsert:true,w:0} ); 

Also auto incrementing _id is required.so how to proved correct index(incremented for new record) ? Any pointers on this would be helpful even using a different driver.however flexible schema is needed.

csv file contents:

1,101,111
2,102,222
2,103,223
3,104,224
3,105,225
4,106,226
4,107,227
4,108,228

code:

var MongoClient = require('mongodb').MongoClient;
var csv = require("fast-csv");
var index=0;

MongoClient.connect('mongodb://127.0.0.1:27017/database', function(err, db) {
    if(err) throw err;

    csv.fromPath("data.csv")
        .on("record", function(data){               
            doc={};
            doc[data[1]]=data[2];
            db.collection("r").update(
                {"rid":Number(data[0])},
                { 
                    "$setOnInsert": {
                        "_id": ++index
                    },
                    "$set":doc
                },
                { "w":0, "upsert" :true }
            );                  
        })      

        .on("end", function(){
            console.log("done");
        });

    });

output:

{ "_id" : 1, "rid" : 1, "101" : "111" }
{ "_id" : 2, "rid" : 2, "102" : "222", "103" : "223" }
{ "_id" : 4, "rid" : 3, "104" : "224", "105" : "225" }
{ "_id" : 6, "rid" : 4, "106" : "226", "107" : "227", "108" : "228" }

required output:

{ "_id" : 1, "rid" : 1, "101" : "111" }
{ "_id" : 2, "rid" : 2, "102" : "222", "103" : "223" }
{ "_id" : 3, "rid" : 3, "104" : "224", "105" : "225" }
{ "_id" : 4, "rid" : 4, "106" : "226", "107" : "227", "108" : "228" }