我正在尝试使用野牛查询MongoDB中具有两个字段的所有JSON数据,但结果为null。

{
        "allowedList": [
            {
                "List": [
                    {
                        "allow": {
                            "ss": 1,
                        },
                        "Information": [
                            {
                                "Id": "Id1"
                            }
                        ]
                    }
                ]
            }
        ]
        }

我能够使用命令行在MongoDB上过滤所有内容
    db.slicedb.find({"allowedList.List.allow.ss":1,"allowedList.List.Information.nsiId":"Id-Id21"})
but using

query := bson.M{"allowedList.List.allow": bson.M{"ss": sst}, "allowedList.List.Information": bson.M{"Id": Id}}

sst和Id是输入到查询函数的整数和字符串
err := db.C(COLLECTION).Find(query).All(&specificSlices)

但无法正常工作,即使存在与两个字段匹配的json数据,也会变为null。有人可以帮我指出我的查询出了什么问题吗?

服务器和数据库配置
type SliceDataAccess struct {
        Server   string
        Database string
   }



var db *mgo.Database

const (
    COLLECTION = "slicedb"
  )

建立与数据库的连接
func (m *SliceDataAccess) Connect() {
        session, err := mgo.DialWithTimeout(m.Server, 20*time.Second)
            if err != nil {
                log.Fatal(err)
            }
            db = session.DB(m.Database)
        }

结构字段
type InstanceInfo struct {
     ID     string    `json:"nfId" bson:"_id"`
     AllowedList []AllowedNssai `json:"allowedList" bson:"allowedList"`

   }

type AllowedNssai struct {
        List []AllowedSnssai `json:"List,omitempty" bson:"List"`
        ...
     }

type AllowedSnssai struct {
      Allow *Snssai `json:"allow,omitempty" bson:"allow"`
      Information []NsiInformation `json:"Information,omitempty" bson:"Information"`

  }

type NsiInformation struct {
      Id string `json:"Id" bson:"Id"`
   }




type Snssai struct {
      Ss int32  `json:"sst" bson:"ss"`

   }

查询功能已定义
func (m *SliceDataAccess) FindAll(sst int32, nsiId string ([]InstanceInfo, error) {
var specificSlices []InstanceInfo

query := bson.M{"allowedList.List.allow": bson.M{"ss": sst}, "allowedList.List.Information": bson.M{"Id": nsiId}}

err := db.C(COLLECTION).Find(query).All(&specificSlices)
if err != nil {
       return specificSlices, err
       }
        return specificSlices, nil
     }

HTTP处理程序功能,用于请求和响应
func AvailabilityGet(w http.ResponseWriter, r *http.Request)
            var slice InstanceInfo
            err := json.NewDecoder(r.Body).Decode(&slice)
            if err != nil {
                respondWithError(w, http.StatusBadRequest, "Object body not well decoded")
                return
            }
            sst := slice.AllowedList[0].List[0].Allow.Sst
            nsiId := slice.AllowedList[0].List[0].Information[0].Id

            specificSlices, err := da.FindAll(sst, nsiId)

            json.NewEncoder(w).Encode(specificSlices)
        }

附件是我完成的完整代码。

最佳答案

这工作

查询:= bson.M {“allowedNssaiList.allowedSnssaiList.allowedSnssai.sst”:sst,“allowedNssaiList.allowedSnssaiList.nsiInformationList.nsiId”:nsiId}

10-06 14:21
查看更多