英文:
Getting all data from mongodb compound collection with filter in golang
问题
我尝试使用API请求体中指定的Name字段来获取所有数据。我为.Find()函数创建了一个过滤器。但是我无法得到任何结果(响应体显示为空,没有任何错误)。你可以在下面看到我的模型文件和代码的其他部分。
控制器代码:
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})
}
模型文件:
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 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
得分: 2
你的过滤器将匹配Patient
是一个嵌入文档,并且具有与给定值匹配的单个Name
字段的文档。
要按嵌入文档的字段进行过滤,您必须使用点表示法:
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:
filter := bson.M{"Patient.Name": ctx.Query("name")}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论