我的 mongoDB 数据库中有这些数据。{ "_id":"5d9ce9fd270eae22adb95d70", ... "isdriver":true, "driver":{ "walletmoney":0, "license":"6eef8271-62d7-4a1c-972a-2c40a773b35a", "vehicle":{ "image":"b6c3619b-86e6-49d0-8734-e2c48815dfc1", "insurance":"5f8229c4-4700-4059-8b72-9344a2bc6092", "manufacturer":"Tesla", "model":"Model 3", "vin":"12345678912345678", "year":2018 }, "verified":false ... }}这是我的驱动程序结构type Driver struct { ... Verified bool `json:"verified,omitempty"` License string `json:"licenseimage,omitempty"` ... Vehicle Vehicle `json:"vehicle,omitempty"`}这是我的学生结构type Student struct { ID primitive.ObjectID `bson:"_id,omitempty"` ... IsDriver bool `json:"isdriver,omitempty"` Driver Driver `json:"driver,omitempty"`}车辆结构type Vehicle struct { Image string `json:"vehicleimage,omitempty"` Insurance string `json:"insuranceimage,omitempty"` VIN string `json:"vin,omitempty"` Manufacturer string `json:"manufacturer,omitemptyr"` <-----(Edit) Find out this is also wrong Model string `json:"model,omitempty"` Year uint16 `json:"year,omitempty"`}但我从邮递员那里得到的答复很荒谬[ { "vehicle": { "manufacturer": "" } }, { "vehicle": { "manufacturer": "" } }]一件事是好的,作为响应,我得到了两个对象,这很好,因为根据我的filter建议isdriver: true,我在数据库中总共有三个文档,其中两个具有isdriver: true.有人能帮我解决这个问题吗?为什么我会收到这样的回复?
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
您正在学生集合中进行查找,但您解码为驱动程序。这需要改变。
var drivers []Driver
var student Student
// Get the next result from the cursor
for cur.Next(context.TODO()) {
err := cur.Decode(&student)
if err != nil {
fmt.Println(err)
}
drivers = append(drivers, student.Driver)
}
此外,您缺少以下字段的Inline结构标记:DriverStudent
type Student struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
IsDriver bool `json:"isdriver,omitempty"`
// Note that Inline is uppercase.
Driver Driver `json:"driver,omitempty" bson:"driver,Inline"`
}
当然,所有引用的结构也是如此。
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报
0/150
提交
取消