node.js - My mongodb code does not insert all data -


i receive stream of /r/n terminated data want inserted mongodb database.

tag~4~keyword~sim tag~5~keyword~mib tag~4~keyword~gom tag~3~keyword~qbo tag~6~keyword~qqq tag~3~keyword~k94 tag~4~keyword~g93 

i've separated them using arr.split(~) i'll array representation first line represented follows:

arr[0] refers tag arr[1] refers 4  arr[2] refers keyword arr[3] refers sim 

i insert them mongodb representation:

{    tag: 4,   keywords: [{ keyword: "sim" }, { keyword: "gom" }, { keyword: "g93"} ] } 

the gist of nodejs/mongo client (native) code using async follows:

i. check collection('dat') presence of tag.
ii. if tag not exist, create document , insert tag number
iii. next insert keywordobj.

var newtagnumber = parseint(arr[1]) var keywordobj = { keyword: arr[3] }     async.waterfall([   function(callback) {     db.collection('dat')             .find({ tag: newtagnumber },              { forceserverobjectid: true}).toarray((err, result) => {                db.collection('dat').createindex({ tag: 1 }, { unique: true })       if (err) return callback(err)       if (!result.length) return callback(null, false)     })  },  function(arg1, callback) {    if (!arg1) {      db.collection('dat')              .insertone({ tag: newtagnumber },               { forceserverobjectid: true }, (err, result2) => {        if (err) return callback(err)      })      } // end if     // insert keyword object correct tag    db.collection('dat')            .updateone({ tag: newtagnumber },            { $push : { keywords: keywordobj }},            { forceserverobjectid: true }, (err, result3) => {              if (err) return callback(err)            })    }],  function(err) {    if (err) {      if (err.message) {        return console.log(err.message)      }      return console.log(err)    }  }) 

using tag 4 example, supposed have 3 keywordobj insertions keywords array. however, getting two.

i'm not sure have gone wrong. due callbacks?

managed figure out!

replaced async.waterfall code

db.collection('dat')   .updateone({ tag: newtagnumber },              { $push : { keywords: keywordobj }},              { upsert: true })   .then((result) => {     console.log('result ' + result)   } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -