douyi4297 2014-01-02 18:20
浏览 43
已采纳

MGO-从Mongo返回的包含结果的空结果

I have a GOLANG struct as follows:

type OrgWhoAmI struct {
FriendlyName            string        `json:"friendlyName"`
RedemptionCode          string        `json:"redemptionCode"`
StartUrls               []StartUrl    `json:"startUrls"`
Status                  string        `json:"status"`
Children                []OrgChildren `json:"childrenReemptionCodes"`
}

type StartUrl struct {
DisplayName string `json:"displayName"`
URL         string `json:"url"`
}

type OrgChildren struct {
FriendlyName   string `json:"childFriendlyName"`
RedemptionCode string `json:"childRedemptionCode"`
}

I've created and successfully inserted records into a MongoDB collection (as I can see the results by querying Mongo with the CLI mongo program) - but when I query with MGO as follows, I get nothing:

func main() {
    session, sessionErr := mgo.Dial("localhost")
defer session.Close()

    // Query All
    collection := session.DB("OrgData").C("orgWhoAmI")
var results []OrgWhoAmI
err = collection.Find(bson.M{}).All(&results)
if err != nil {
    panic(err)
}
for _, res := range results {
    fmt.Printf("Result: %s|%s
", res.FriendlyName, res.RedemptionCode)
}
}

The results printed are:

Result: | Result: | Result: | Result: |

If I ask for the count for records, I get the correct number, but all values for all fields are blank. Not sure what I'm missing here.

  • 写回答

1条回答 默认 最新

  • donglao7947 2014-01-02 19:15
    关注

    If you aren't creating them in go, it's probably not serializing the key names for you properly. The default for bson is to lowercase the keys, so you need to specify it if you want something else. Also note that you have a typo in OrgWhoAmI for json:"childrenReemptionCodes" (should be Redemption, I'm guessing). You can specify both bson and json separately if you want them to be different.

    type OrgWhoAmI struct {
       FriendlyName            string        `bson:"friendlyName" json:"friendlyName"`
       RedemptionCode          string        `bson:"redemptionCode" json:"redemptionCode"`
       StartUrls               []StartUrl    `bson:"startUrls" json:"startUrls"`
       Status                  string        `bson:"status" json:"status"`
       Children                []OrgChildren `bson:"childrenRedemptionCodes" json:"childrenRedemptionCodes"`
    }
    
    type StartUrl struct {
       DisplayName string `bson:"displayName" json:"displayName"`
       URL         string `bson:"url" json:"url"`
    }
    
    type OrgChildren struct {
       FriendlyName   string `bson:"childFriendlyName" json:"childFriendlyName"`
       RedemptionCode string `bson:"childRedemptionCode" json:"childRedemptionCode"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题