title: Printing all records in a mongoDB Collection Golang
body: <p>I've got a MongoDB Collection <code>collection := db.Collection("JobBacklog")</code> that I'm trying to print out into the console. The DB is in a Docker container and controlling it with a script written in Go. </p>
  
  <p>From what I've been able to find from the mongo-go-driver <a href="https://godoc.org/github.com/mongodb/mongo-go-driver/mongo" rel="nofollow noreferrer">https://godoc.org/github.com/mongodb/mongo-go-driver/mongo</a>
  there is a way to do this but my code keeps returning <code>document is nil</code> when I know it isn't. </p>
  
  <p>This is my code I'm using to try to iterate through a collection called <code>JobBacklog</code></p>
  
  <pre><code>cur, err := collection.Find(context.Background(), nil)
          if err != nil {
              log.Fatal(err)
          }
          defer cur.Close(context.Background())
          for cur.Next(context.Background()) {
              raw, err := cur.DecodeBytes()
              if err != nil {
                  log.Fatal(err)
              }
              //print element data from collection
              fmt.Println("Element", raw, x)
          }
          if err := cur.Err(); err != nil {
              log.Fatal(err)
          }
  </code></pre>
  
  <p>I expect it to print out the contents of the collection which are: </p>
  
  <pre><code>_id:5c2d34e36657ba3238374f9a
  UID:"ALDK"
  PROFILE:"B"
  STATUS:"PENDING"
  DEVICE:"2.2.2.2"
  </code></pre>
  
  <p>That is an example entry of the JobBacklog DB. </p>
  
  <p>Full disclosure, the end goal for this is to find the last entry that was added to the collection, but I need to be able to read through the collection first. </p>
  
  <p>I know I'm connected to the DB, I can add/find/delete entries, but the printing out of all in the collection is eluding me. 
  Any assistance is appreciated. 
  Thanks! </p>