我正在嘗試使用 Go 接口進行動態 mongodb 查詢,如下所示func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, args ...interface{}) ([]model.NfProfile, bool) { var ip []model.NfProfile pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args, "allowedNssais.sd": args} filter := bson.M{"ipv4Addresses": true} err := db.C(COLLECTION).Find(pipeline).Select(filter).All(&ip) if err != nil { return ip, false } return ip, true}當我在我的 http 服務器處理程序中使用該函數時沒有錯誤 nfInstanceIp, success := Da.FindIp(targetNfType, sst, sd) if !success { WriteError(response, ErrInternalServer) return }但響應 nfInstanceIp 始終為空,即使我的 mongodb 集合中有與 FindIp 參數匹配的值。當我在下面的代碼中使用其他類型(如整數和字符串)時,一切正常。func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, sst int, sd string) ([]model.NfProfile, bool) { var ip []model.NfProfile pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": sst, "allowedNssais.sd": sd} filter := bson.M{"ipv4Addresses": true} err := db.C(COLLECTION).Find(pipeline).Select(filter).All(&ip) if err != nil { return ip, false } return ip, true}誰能向我解釋為什么使用接口不起作用以及如何動態編寫這個函數?按照建議修改函數以使用 mongodb $ 或如下代碼所示的邏輯func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, args ...interface{}) ([]model.NfProfile, bool) { var ip []model.NfProfile pipeline := bson.M{ "nfType": preferredNfInstances, "$or": []interface{}{ bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}, bson.M{"amfInfo.taiList.tac": args}, bson.M{"smfInfo.taiList.tac": args}, bson.M{"upfInfo.taiList.tac": args}, }, }邏輯 $or 不起作用。只有當我的 FindIp 輸入匹配時才有效bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}但即使將類型轉換設置為 args,其他輸入也不起作用知道如何在這種情況下使用 mongodb 邏輯 $or 嗎?
1 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
您需要執行索引,如果需要,還需要進行類型轉換。
pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args[0].(int), "allowedNssais.sd": args[1].(string)}
或者
pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args[0], "allowedNssais.sd": args[1]}
- 1 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消