I have this function which uses the official mongo-go-driver (https://github.com/mongodb/mongo-go-driver)
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(&result)
if err != nil { log.Fatal(err) }
fmt.Println("value of found parsed")
fmt.Println(result)
returnBson = append(returnBson, result)
}
}
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 $eq
operator for only a specific field.
This works for the condition where the subItemKey is empty. Like this:
database.FindItemMongo(dataStruct, "", "users")
Gives
api | 1:36:55 app | value of found parsed
api | map[_id:ObjectID("8494398fsfd") data:map[email:qwer password:wer token:...]]
However, if I use the command:
database.FindItemMongo(dataStruct, "Email", "users")
I get nothing.
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?
EDIT
I should mention that the result of
unmarshalledSearch := bson.M{"data": bson.M{strings.ToLower(subItemKey): bson.M{"$eq": subItemString}}}
fmt.Println("value of unmarshalledSearch")
fmt.Println(unmarshalledSearch)
is
api | value of unmarshalledSearch
api | map[data:map[email:map[$eq:qwer]]]
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?