title: Pivot rows to columns in MongoDB
body: <p>The relevant question is <a href="https://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server-2008">Efficiently convert rows to columns in sql server</a>. But the answer is specific to SQL.</p>
  
  <p>I want the same result i.e. pivot row to column without aggregating anything (as of now) in MongoDB.</p>
  
  <p>The collection looks something as below. These are statistics of facebook page properties:</p>
  
  <pre>
  
  timestamp | propName | propValue
  --------------------------------
  1371798000000 | page_fans | 100
  --------------------------------
  1371798000000 | page_posts | 50
  --------------------------------
  1371798000000 | page_stories | 25
  --------------------------------
  </pre>
  
  <p>I need answer like:</p>
  
  <pre>
  timestamp | page_fans | page_posts | page_stories
  --------------------------------
  1371798000000 | 100 | 50 | 25
  --------------------------------
  </pre>
  
  <p>The column names are pre-determined. They don't have to be generated dynamically. But question is how to achieve this in MongoDB.</p>
  
  <p>I believe aggregation is of no use for this purpose. Do I need to use MapReduce? But in that case I have nothing to reduce I guess? Well another option could be fetching these values in code and do the manipulation in programming language e.g. Java</p>
  
  <p>Any insights would be helpful. Thanks in advance :)!!!</p>
  
  <p>EDIT (Based on input from Schaliasos):</p>
  
  <p>Input JSON:</p>
  
  <pre><code>{
          "_id" : ObjectId("51cd366644aeac654ecf8f75"),
          "name" : "page_storytellers",
          "pageId" : "512f993a44ae78b14a9adb85",
          "timestamp" : NumberLong("1371798000000"),
          "value" : NumberLong(30871),
          "provider" : "Facebook"
  }
  {
          "_id" : ObjectId("51cd366644aeac654ecf8f76"),
          "name" : "page_fans",
          "pageId" : "512f993a44ae78b14a9adb85",
          "timestamp" : NumberLong("1371798000000"),
          "value" : NumberLong(1291509),
          "provider" : "Facebook"
  }
  {
          "_id" : ObjectId("51cd366644aeac654ecf8f77"),
          "name" : "page_fan_adds",
          "pageId" : "512f993a44ae78b14a9adb85",
          "timestamp" : NumberLong("1371798000000"),
          "value" : NumberLong(2829),
          "provider" : "Facebook"
  }
  </code></pre>
  
  <p>Expected Output JSON:</p>
  
  <pre><code>{
          "timestamp" : NumberLong("1371798000000"),
          "provider" : "Facebook",
          "page_storytellers" : NumberLong(30871),
          "page_fans" : NumberLong("1371798000000"),
          "page_fan_adds" : NumberLong("1371798000000")
  }
  </code></pre>