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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图