3

I try to make get all data with Name field where I specify in API Request Body. I made a filter for .Find() function. But I can't get any result (response body says null, No errors at all). You can see my model file and other parts of the code at bottom.

Controller:

func GET_FormByPatientFullName(ctx *gin.Context) {
   col := mongodb.CLIENT.Database(config.DATABASE_NAME).Collection("consentforms")
   filter := bson.M{"Patient": bson.M{"Name": ctx.Query("name")}}

   cursor, err := col.Find(_CONTEXT.TODO(), filter)
   if err != nil {
      log.Fatal(err)
   }

   var results []general_models.ConsentForm
   if err = cursor.All(_CONTEXT.TODO(), &results); err != nil {
      log.Fatal(err)
   }
   for _, result := range results {
      res, _ := json.Marshal(result)
      fmt.Println(string(res))
   }
   ctx.IndentedJSON(http.StatusOK, gin.H{"data": results})
}

Model File:

type ConsentForm struct {
   ID                 primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
   FormFileURL        string             `json:"FormFileURL" bson:"FormFileURL"`
   ProcessName        string             `json:"ProcessName" bson:"ProcessName"`
   DateOfNotification string             `json:"DateOfNotification" bson:"DateOfNotification"`
   WitnessName        string             `json:"WitnessName" bson:"WitnessName"`
   WitnessSurname     string             `json:"WitnessSurname" bson:"WitnessSurname"`
   ResponsibleDoctor  string             `json:"ResponsibleDoctor" bson:"ResponsibleDoctor"`
   Patient            IPatient           `json:"Patient" bson:"Patient"`
   QuestionOptions    IQuestionOptions   `json:"QuestionOptions" bson:"QuestionOptions"`
   AdditionalDetails  string             `json:"AdditionalDetails" bson:"AdditionalDetails"`
}

type IPatient struct {
   // ID                primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
   Name              string `json:"Name" bson:"Name"`
   Surname           string `json:"Surname" bson:"Surname"`
   Birthdate         string `json:"Birthdate" bson:"Birthdate"`
   TCKN              string `json:"TCKN" bson:"TCKN"`
   FacePhotoURL      string `json:"FacePhotoURL" bson:"FacePhotoURL"`
   SignatureImageURL string `json:"SignatureImageURL" bson:"SignatureImageURL"`
}

I tried to filter and get all the data of the user according to the user name. But I think I have a mistake in the filter part or in the overall code because I can't get any data return. I get an empty return.

1 Answer 1

2

Your filter would match documents where Patient is an embedded document with a single Name field matching the given value.

To filter by a field of an embedded document, you have to use the dot notation:

filter := bson.M{"Patient.Name": ctx.Query("name")}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much <3 But I have a question, why can't we write bson.M blocks like compound JSON. Is this only way to solve this?
@PoyrazHANCILAR As written in the answer: your filter defines a condition for the whole Patient field (which is an embedded document), not just for a field of it. This is how MongoDB works.
MongoDB is schemaless, different documents in the same collection may have different fields. Your filter is how you could query documents where Patient has only a single Name field with the given value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.