【问题标题】:MongoDB bson.M queryMongoDB bson.M 查询
【发布时间】:2019-08-10 22:26:20
【问题描述】:

我正在尝试使用野牛查询 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)
        }

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

【问题讨论】:

  • 包含你的 Go 代码。找不到我们看不到的错误是不可能的。
  • 您的工作命令行示例使用nsiId,这是一个错字吗?应该是Id
  • 是的,应该是Id,错字

标签: mongodb go


【解决方案1】:

成功了

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

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 2021-01-24
    • 2016-07-21
    • 1970-01-01
    • 2019-02-18
    • 2019-06-18
    • 2015-06-24
    • 2014-02-22
    相关资源
    最近更新 更多