title: Golang MongoDB not returning on conditional query
body: <p>I have this function which uses the official mongo-go-driver (<a href="https://github.com/mongodb/mongo-go-driver" rel="nofollow noreferrer">https://github.com/mongodb/mongo-go-driver</a>)</p>
  
  <pre><code>func FindItemMongo(dataStruct interface{}, subItemKey string, collectionName string)(){
      fmt.Println("inside FindDataMongo in Controller")
      localDB := DB
      coll := localDB.Collection(collectionName)
      ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
      var searchData []uint8
      if subItemKey==""{
          fmt.Println("inside no key searchData")
          searchData, _ = bson.Marshal(bson.M{"data": dataStruct})
          fmt.Println("value of searchData")
          fmt.Println(searchData)
          fmt.Println("value of reflect type of searchData")
          fmt.Println(reflect.TypeOf(searchData))
      }else{
          reflectItem := reflect.ValueOf(dataStruct)
          subItem := reflectItem.FieldByName(subItemKey)
          subItemString := subItem.Interface().(string)
          searchData, _ =  bson.Marshal(bson.M{"data": bson.M{strings.ToLower(subItemKey): bson.M{"$eq": subItemString}}})
          fmt.Println("value of searchData")
          fmt.Println(searchData)
          fmt.Println("value of reflect type of searchData")
          fmt.Println(reflect.TypeOf(searchData))
      }
      cursor, err := coll.Find(ctx, searchData)
      if err != nil {
          log.Fatal(err)
      }
      defer cursor.Close(ctx)
      var returnBson []map[string]interface{}
      var result bson.M
      for cursor.Next(ctx) {
          err := cursor.Decode(&amp;result)
          if err != nil { log.Fatal(err) }
          fmt.Println("value of found parsed")
          fmt.Println(result)
          returnBson = append(returnBson, result)
      }
  }
  </code></pre>
  
  <p>Which is attempting to find data that is in the database for two conditions, related to the if statement. If the subItemKey is not defined, then it searches for an exact match to the passed in struct, if the key is defined, then it searches for a match in the data using the <code>$eq</code> operator for only a specific field. </p>
  
  <p>This works for the condition where the subItemKey is empty. Like this: </p>
  
  <pre><code>database.FindItemMongo(dataStruct, "", "users")
  </code></pre>
  
  <p>Gives </p>
  
  <pre><code>api       | 1:36:55 app         | value of found parsed
  api       | map[_id:ObjectID("8494398fsfd") data:map[email:qwer password:wer token:...]]
  </code></pre>
  
  <p>However, if I use the command: </p>
  
  <pre><code>database.FindItemMongo(dataStruct, "Email", "users")
  </code></pre>
  
  <p>I get nothing. </p>
  
  <p>Something is not correct with how I define searchData in the else statement and I'm not sure what. Does anyone see what is going wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I should mention that the result of </p>
  
  <pre><code>    unmarshalledSearch := bson.M{"data": bson.M{strings.ToLower(subItemKey): bson.M{"$eq": subItemString}}}
      fmt.Println("value of unmarshalledSearch")
      fmt.Println(unmarshalledSearch)
  </code></pre>
  
  <p>is </p>
  
  <pre><code>api       | value of unmarshalledSearch
  api       | map[data:map[email:map[$eq:qwer]]]
  </code></pre>
  
  <p>which is the same format as the data that is coming back from the empty field query. This is the crux of the problem - if the searchData appears to be correctly formatted, why is the cursor not returning with any results?</p>