title: mongoose schema check if model instance is saved for the first time
body: <p>I have 3 schemas, 2 of which have a <code>.pre('save')</code> hook to push its <code>_id</code> into the previous one's. You can take the example of a forum with topics, questions and comments</p>
  
  <pre><code>var topicSchema = new Schema({
    arr: {type:[Schema.ObjectId], ref:'Question'},
  });
  var Topic = new mongoose.model('Topic', topicSchema);
  
  var questionSchema = new Schema({
    targetId: {type:Schema.ObjectId, ref:'Topic', required:true},
    arr: {type:[Schema.ObjectId], ref:'Comment'},
  });
  var Question = new mongoose.model('Question', questionSchema);
  
  var commentSchema = new Schema({
    targetId: {type:Schema.ObjectId, ref:'Question', required:true},
  });
  var Comment = new mongoose.model('Comment', commentSchema);
  </code></pre>
  
  <p>Now for the above schema I want: When I save a question I want to automatically push the question's <code>_id</code> to its respective <code>Topic.arr</code> and when I save a comment to auto push its it to its respective <code>Question.arr</code></p>
  
  <p>I've tried solving this using <code>.pre('save')</code> hooks as follows:</p>
  
  <pre><code>function addPreSave (schema, idProperty, containerProperty) {
    var modelName = schema.paths[idProperty].options.ref;
    var model = mongoose.models[modelName];
  
    schema.pre('save', function (next) {
      model.findById(this[idProperty], function (err, doc) {
        doc[containerProperty].push(this._id);
        doc.save(next);
      }.bind(this));
    });
  }
  
  addPreSave(questionSchema, 'targetId', 'arr');
  addPreSave(commentSchema, 'targetId', 'arr');
  </code></pre>
  
  <p>All that the above function does (and the 2 calls to it) is to add a <code>.pre('save')</code> hook to each of the 2 schemas to add their <code>_id</code> in their respective parent.</p>
  
  <hr>
  
  <p><strong>THE ISSUE:</strong> The problem is that now, every time I save a comment, it pushes its id to the topic, but I only actually want to do this the very first time only. So in the above example if you create a topic, then a question for it and then a comment for the question, the <code>Topic.arr</code> will have 2 ids in it (twice the question's <code>_id</code>) as it saves once for the question and a second time it's triggered by the comment's call to save the question</p>
  
  <p>Does anyone know a way around this, or more specifically if you know how to figure out in the pre-save hook if this is a first-time save or not?</p>