从Golang中的MongoDB复合集合中获取带有过滤器的所有数据。

huangapple go评论127阅读模式
英文:

Getting all data from mongodb compound collection with filter in golang

问题

我尝试使用API请求体中指定的Name字段来获取所有数据。我为.Find()函数创建了一个过滤器。但是我无法得到任何结果(响应体显示为空,没有任何错误)。你可以在下面看到我的模型文件和代码的其他部分。

控制器代码:

  1. func GET_FormByPatientFullName(ctx *gin.Context) {
  2. col := mongodb.CLIENT.Database(config.DATABASE_NAME).Collection("consentforms")
  3. filter := bson.M{"Patient": bson.M{"Name": ctx.Query("name")}}
  4. cursor, err := col.Find(_CONTEXT.TODO(), filter)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. var results []general_models.ConsentForm
  9. if err = cursor.All(_CONTEXT.TODO(), &results); err != nil {
  10. log.Fatal(err)
  11. }
  12. for _, result := range results {
  13. res, _ := json.Marshal(result)
  14. fmt.Println(string(res))
  15. }
  16. ctx.IndentedJSON(http.StatusOK, gin.H{"data": results})
  17. }

模型文件:

  1. type ConsentForm struct {
  2. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  3. FormFileURL string `json:"FormFileURL" bson:"FormFileURL"`
  4. ProcessName string `json:"ProcessName" bson:"ProcessName"`
  5. DateOfNotification string `json:"DateOfNotification" bson:"DateOfNotification"`
  6. WitnessName string `json:"WitnessName" bson:"WitnessName"`
  7. WitnessSurname string `json:"WitnessSurname" bson:"WitnessSurname"`
  8. ResponsibleDoctor string `json:"ResponsibleDoctor" bson:"ResponsibleDoctor"`
  9. Patient IPatient `json:"Patient" bson:"Patient"`
  10. QuestionOptions IQuestionOptions `json:"QuestionOptions" bson:"QuestionOptions"`
  11. AdditionalDetails string `json:"AdditionalDetails" bson:"AdditionalDetails"`
  12. }
  13. type IPatient struct {
  14. // ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  15. Name string `json:"Name" bson:"Name"`
  16. Surname string `json:"Surname" bson:"Surname"`
  17. Birthdate string `json:"Birthdate" bson:"Birthdate"`
  18. TCKN string `json:"TCKN" bson:"TCKN"`
  19. FacePhotoURL string `json:"FacePhotoURL" bson:"FacePhotoURL"`
  20. SignatureImageURL string `json:"SignatureImageURL" bson:"SignatureImageURL"`
  21. }

我尝试根据用户名称筛选和获取所有用户数据。但是我认为在过滤器部分或整体代码中有错误,因为我无法获取任何数据返回。我得到了一个空的返回结果。

英文:

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:

  1. func GET_FormByPatientFullName(ctx *gin.Context) {
  2. col := mongodb.CLIENT.Database(config.DATABASE_NAME).Collection("consentforms")
  3. filter := bson.M{"Patient": bson.M{"Name": ctx.Query("name")}}
  4. cursor, err := col.Find(_CONTEXT.TODO(), filter)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. var results []general_models.ConsentForm
  9. if err = cursor.All(_CONTEXT.TODO(), &results); err != nil {
  10. log.Fatal(err)
  11. }
  12. for _, result := range results {
  13. res, _ := json.Marshal(result)
  14. fmt.Println(string(res))
  15. }
  16. ctx.IndentedJSON(http.StatusOK, gin.H{"data": results})
  17. }

Model File:

  1. type ConsentForm struct {
  2. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  3. FormFileURL string `json:"FormFileURL" bson:"FormFileURL"`
  4. ProcessName string `json:"ProcessName" bson:"ProcessName"`
  5. DateOfNotification string `json:"DateOfNotification" bson:"DateOfNotification"`
  6. WitnessName string `json:"WitnessName" bson:"WitnessName"`
  7. WitnessSurname string `json:"WitnessSurname" bson:"WitnessSurname"`
  8. ResponsibleDoctor string `json:"ResponsibleDoctor" bson:"ResponsibleDoctor"`
  9. Patient IPatient `json:"Patient" bson:"Patient"`
  10. QuestionOptions IQuestionOptions `json:"QuestionOptions" bson:"QuestionOptions"`
  11. AdditionalDetails string `json:"AdditionalDetails" bson:"AdditionalDetails"`
  12. }
  13. type IPatient struct {
  14. // ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  15. Name string `json:"Name" bson:"Name"`
  16. Surname string `json:"Surname" bson:"Surname"`
  17. Birthdate string `json:"Birthdate" bson:"Birthdate"`
  18. TCKN string `json:"TCKN" bson:"TCKN"`
  19. FacePhotoURL string `json:"FacePhotoURL" bson:"FacePhotoURL"`
  20. SignatureImageURL string `json:"SignatureImageURL" bson:"SignatureImageURL"`
  21. }

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

得分: 2

你的过滤器将匹配Patient是一个嵌入文档,并且具有与给定值匹配的单个Name字段的文档。

要按嵌入文档的字段进行过滤,您必须使用点表示法:

  1. filter := bson.M{"Patient.Name": ctx.Query("name")}
英文:

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:

  1. filter := bson.M{"Patient.Name": ctx.Query("name")}

huangapple
  • 本文由 发表于 2022年12月10日 19:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74752419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定