douzong5473 2019-03-20 09:14
浏览 397

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.

  • 写回答

1条回答 默认 最新

  • duanpo7282 2019-03-20 12:31
    关注

    this worked

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

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料