How to force a synchronous call to Mongoose Save ()

I am writing a script in Node.js that should do the following:

  • Open xml file
  • For each node in the file
  • Do a mongodb search to try to find the object related to this node
  • if the object is not found, create it; otherwise, process the found object in some way.
  • save the (possibly new) object back to the database.
  • go to step 2

I looked at this for a while and came to the conclusion that it is almost impossible to do with asynchronous mongodb. There are several problems, but, for example, if you are dealing with 20,000 of these nodes, then for this, async will hang a script. However, it is impossible to make them as a batch insertion either because of step 4, which must be sought if the object already exists or not.

You could rake something terrible together that caches the created objects and then save them as something like step 7, except that it would be difficult, because there are several collections that the objects go to, and you need try first find the objects from the cache, then the database, in step 4. If this is a solution, I will just write Javascript as broken and write it in perl instead. So my question is, for something as simple as the above sequence of actions, can I somehow make mongodb be synchronous so that my script does not turn into madness? I want to be able to say document.save () (by the way, I'm using Mongoose) and then not return it until it is saved.

Edit: code added

20000 . ( ), , 200 000 script, ( 1,5 ). hObj.save(); , , .

    models('hs').findOne({name: r2.$.name}, function (err, h) {
    if (err) {
        console.log(err);
    } else {
        var resultObj = createResult(meeting, r1, r2);

        if (h == undefined) {

            var hObj = new models('hs')({
                name : r2.$.name,
                results : [resultObj],
                numResults : 1
            });

            hObj.save();
        } else {
            h.results.push(resultObj);
            h.numResults++;
            h.save();
        }
    }
});
+4
1

async github:

eachSeries (arr, , )

, , arr . , . , .

, , XML

async.eachSeries(
  nodes,
  // This will be applied to every node in nodes
  function (node, callback) {
    models('hs').findOne({name: r2.$.name}, function (err, h) {
      if (err) {
        console.log(err);
      } else {
        // Async?
        var resultObj = createResult(meeting, r1, r2);

        if (h == undefined) {

          var hObj = new models('hs')({
            name : r2.$.name,
            results : [resultObj],
            numResults : 1
          });

          hObj.save(function (err, p) {
            // Callback will tell async that you are done
            callback();
          });
        } else {
          h.results.push(resultObj);
          h.numResults++;
          h.save(function (err, p) {
            // Callback will tell async that you are done
            callback();
          });
        }
      }
    });
  },
  // This will be executed when all nodes has been processed
  function (err) {
    console.log('done!');
  }
);
+5

Source: https://habr.com/ru/post/1542755/


All Articles