title: mongoose update with push operations on array and set operation on object
body: <p>I have this mongoose schema</p>
  
  <pre><code>var ContactSchema = module.exports = new mongoose.Schema({
    name: {
      type: String,
      required: true
    },
    phone: {
      type: Number,
      required: true,
    },
    messages: [
    {
      title: {type: String, required: true},
      msg: {type: String, required: true}
    }],
    address:{ city:String,
              state:String
    }
  });
  </code></pre>
  
  <p>I have initially the collection set with name and phone field. I need to update the collection with new messages into messages array and new address into address object. the function must also need to handle any single operation, ie in some case i have only update to messages array or updates to both name and address. so how i can i do all operations in a single function.</p>
  
  <pre><code>        var messages= {
              title: req.body.title,
              msg: req.body.msg
          }
          Model.findOneAndUpdate({'_id': req.body.id,},{$push: {messages:message}},{upsert: true}, function (err, data) {
              if (err) {
                  return res.status(500).send(err);
              }
              if (!data) {
                  return res.status(404).end();
              }
              return res.status(200).send(data);
          });
  </code></pre>