MongoDB bson.M查询

I am trying to query using bison all JSON data in MongoDB with two fields but am getting null as result.

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

I was able to filter all using the MongoDB at command line using

    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 and Id are integer and string input to the query function

err := db.C(COLLECTION).Find(query).All(&specificSlices)

but is not working, am getting null even though there are json data that match the two field. Can someone help point out what was wrong with my query?

Server and database config

type SliceDataAccess struct {
        Server   string
        Database string
   }



var db *mgo.Database

const (
    COLLECTION = "slicedb"
  )

Establish a connection to database

func (m *SliceDataAccess) Connect() {
        session, err := mgo.DialWithTimeout(m.Server, 20*time.Second)
            if err != nil {
                log.Fatal(err)
            }
            db = session.DB(m.Database)
        }

Structs fields

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"`

   }

Query function defined

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 handler function for request and response

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)
        }

Attached is my the full go code i have done.

dongxiong5546
dongxiong5546 是的,应该是Id,是一个错字
一年多之前 回复
drsxobip501258
drsxobip501258 您的工作命令行示例使用nsiId,这是错字吗?应该是Id。
一年多之前 回复
duanjuduo4573
duanjuduo4573 包括您的Go代码。找不到我们看不到的错误是不可能的。
一年多之前 回复

1个回答



这有效</ p>

查询:= bson.M {“ allowedNssaiList.allowedSnssaiList.allowedSnssai.sst “:sst,” allowedNssaiList.allowedSnssaiList.nsiInformationList.nsiId“:nsiId} </ p>
</ div>

展开原文

原文

this worked

query := bson.M{"allowedNssaiList.allowedSnssaiList.allowedSnssai.sst": sst, "allowedNssaiList.allowedSnssaiList.nsiInformationList.nsiId": nsiId}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐