我try 让Get All Data With Name字段在API请求正文中指定的位置.我为.Find()函数制作了一个过滤器.但我无法获得任何结果(响应正文显示为空,根本没有错误).您可以在底部看到我的模型文件和代码的其他部分.

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"`
}

我试图根据用户名过滤并获取该用户的所有数据.但我认为我在筛选器部分或整个代码中有错误,因为我无法获得任何数据返回.我得到的回报是空的.

推荐答案

您的筛选器将匹配其中Patient是具有与给定值匹配的单个Name字段的嵌入文档的文档.

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

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

Mongodb相关问答推荐

MongoDB 4.4如何使用未知密钥更新dict

会话的Mongo-go驱动程序版本.Copy()

如何在Golang保存到MongoDB时排除空数据值的问题?

Tableau 与 Mongo DB Atlas by MongoDB 的连接缓存问题

Mongo 删除最后的文件

将 MongoDB BsonDocument 转换为字符串

如何在 Mongo 聚合中合并文档中的数组字段

在 Heroku 上使用 Node 读取、写入和存储 JSON

mongodump 是否锁定数据库?

使用 ObjectId.GenerateNewId() 还是离开 MongoDB 创建一个?

如何在 MongoDB 集合中查找与给定条件匹配的文档和单个子文档

Express 无法 PUT/DELETE 方法.出了什么问题?

RoR3 上的 Mongoid:1)如何在查询中返回特定字段? 2)需要什么 inverse_of ?

Mongoose.pre('save') 不会触发

MongoDB 按数组元素对文档进行排序

无法使用命令写入模式错误,降级到兼容模式

PyMongo 中的警告消息:count is deprecated

如何在 Ubuntu 上安装 mongodb-clients 最新版本?

与 mongoose/node.js 共享数据库连接参数的最佳方式

MongoDB Compass 中 JSON 输入意外结束