title: Aggregate and flatten an array field in MongoDB
body: <p>I have a Schema:</p>
  
  <pre><code>var ProjectSchema = new Schema({
      name: {
          type: String,
          default: ''
      },
      topics: [{
          type: Schema.ObjectId,
          ref: 'Topic'
      }],
      user: {
          type: Schema.ObjectId,
          ref: 'User'
      }
  });
  </code></pre>
  
  <p>What I want to do is get an array with all topics from all projects. I cannot query Topic directly and get a full list because some topics are unassigned and they do not hold a reference back to a Project (for reasons of avoiding two way references). So I need to query Project and aggregate some how. I am doing something like:</p>
  
  <pre><code>Project.aggregate([{$project:{topics:1}}]);
  </code></pre>
  
  <p>But this is giving me an array of Project objects with the topics field. What I want is an array with topic objects.</p>
  
  <p>How can I do this?</p>