title: How to set a objectId property in mongoose?
body: <p>I have a schema named as Message like below;</p>
  
  <pre><code>Message = new Schema({
      id: Number,
      sender: ObjectId,
      receiver: ObjectId,
      date: Date,
      content: String,
      type: Number
  });
  </code></pre>
  
  <p>And a User schema like below;</p>
  
  <pre><code>User = new Schema({
   id: Number,
   username: String,
   isOnline: Boolean
  });
  </code></pre>
  
  <p>I would like to set the sender and receiver properties of message item when a new message is received. When a message is received I am creating a new Message document and setting its properties. But I do not know how to set sender and receiver properties (which should be a reference to an entity in User document). The code I'm using is like below. You can judge the code as much as you want, that's what I would like to. I do not know the right way to get rid of it. But it seems that this is not the one. When I'm using the code below I could not see the sender and receiver properties are set. I think that it could be because findOne is async (I do not know if it is).</p>
  
  <pre><code>var m = new Message();
  m.content = messageData.text;
  m.id = messageId;
  User.findOne({id: socket.userId}, function (e, o) {
    m.sender = o;
  });
  
  User.findOne({id: messageData.userId}, function (e, o) {
    m.receiver = o;
  });
  
  m.save(function (e) {
    if (!e) {
      console.log("message is saved to the mongodb...");
    }
  });
  </code></pre>
  
  <p>It would be great that if you can tell the right way to do this.</p>
  
  <p>Thanks in advance.</p>