Trying to do a bulk upsert with Mongoose. What's the cleanest way to do this? : <p>I have a collection that holds documents that contains three fields: first_name, last_name, and age. I'm trying to figure out what query in Mongoose I can use to do a bulk upsert. My app is occasionally receiving a new array of objects with those same three fields. I want the query to check if the first AND last name already exist within a document, and if they do - update the age if it's different. Otherwise, if the first and last name don't exist, insert a new document.</p>

<p>Currently, I'm only doing the import - and haven't yet built out the logic for this upsert piece. </p>

<pre><code>app.post('/users/import', function(req, res) {
  let data = req.body;
  let dataArray = [];
  data.forEach(datum =&gt; {
    dataArray.push({
        first: datum.first,
        last: datum.last,
        age: datum.age
    })
})

User.insertMany(dataArray, answer =&gt; {
    console.log(`Data Inserted:`,answer)
})
</code></pre>

<p>`</p>

<p>And my User model looks like this:</p>

<pre><code>const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
  first: String,
  last: String,
  age: Number,
  created_at: { type: Date, default: Date.now }
});

var User = mongoose.model('User', userSchema);
module.exports = User;
</code></pre>
