title: How can I upsert multiple objects with MongoDB & Node.js?
body: <p>Let's say I have an array of Movie genres like so:</p>
  
  <pre><code>[ 
      { id: 28, name: 'Action' },
      { id: 12, name: 'Adventure' },
      { id: 16, name: 'Animation' },
      { id: 35, name: 'Comedy' },
      { id: 80, name: 'Crime' },
      { id: 99, name: 'Documentary' },
      { id: 18, name: 'Drama' },
      { id: 10751, name: 'Family' },
      { id: 14, name: 'Fantasy' },
      { id: 10769, name: 'Foreign' },
      { id: 36, name: 'History' },
      { id: 27, name: 'Horror' },
      { id: 10402, name: 'Music' },
      { id: 9648, name: 'Mystery' },
      { id: 10749, name: 'Romance' },
      { id: 878, name: 'Science Fiction' },
      { id: 10770, name: 'TV Movie' },
      { id: 53, name: 'Thriller' },
      { id: 10752, name: 'War' },
      { id: 37, name: 'Western' }
  ]
  </code></pre>
  
  <p>and I have a connection to a MongoDB (v3.2) instance: <code>db</code>, and I'm using the standard mongodb Node.js driver (<code>const mongodb = require('mongodb').MongoClient</code>).</p>
  
  <p>What I want to be able to do is one bulk upsert operation onto a collection, say <code>genres</code>, where the <code>_id</code> field maps to the <code>id</code> field of our genre objects.</p>
  
  <p>Now, I know I could loop through each item in the array, and do a simple upsert:</p>
  
  <pre><code>for (let i = 0; i &lt; genres.length; i++) {
    await db.collection('genres').update(
      { _id: genres[i].id },
      genres[i],
      { upsert: true }
    );
  }
  </code></pre>
  
  <p>But this feels <em>wasteful</em> and wrong.</p>
  
  <p>Is there an easier way to do what should be a relatively simple task?</p>
  
  <p>Thanks</p>