Saturday, 6 May 2017

add text file values to mongodb

I have a local text file ("results.txt") which has a format of:

256328,899087
721299,109901
310101,001735

The first number in each line represents an index number under the "position" sub document, while the second number represents an index number for the user (not in the position sub document - see below)

     var User = new Schema({
        _id: ObjectId,
        name: String,
        index_number:Number,
        matching:Number,
    position: [{ 
        title: String,
        index_number: Number,
        matching:Number,
}],

What I need to do is loop through the text file, and for each of the first numbers on the line, find the position subdocument which matches this number, then take the second number and insert it into the "matching" field.

What I have so far:

  fs.readFile("result.txt", 'utf8', function(err, data) {

        var arrLines = data.split("\n");
        console.log(arrLines[0]); // which will return 256328,899087 but I need 

var index_number = arrLines.map(function(element) { return element.index_number })
    User.update({"position.$.index_number" : { "$in" : index_number }}{
    "push":{
      // I'm stuck here
    }
  }



via user

No comments:

Post a Comment