小编Soh*_*ikh的帖子

如何检索[]bson.M类型的地图

如何检索多维[]bson.M类型的地图

mongo中的数据就像

"taskData" : { 
    "createdOn" : ISODate("2016-02-20T21:23:11.903Z"), 
    "Task_content" : "@bob", 
    "Priority" : "2", 
    "owner_Uname" : "alice"
}
Run Code Online (Sandbox Code Playgroud)

我尝试访问它的代码

var n []bson.M
 e := collection.Find(bson.M{"users."+strconv.Itoa(j)+".user_name" :   r.FormValue("value[userName]")}).Select(bson.M{"taskData.owner_Uname":1,"_id":0}).All(&n)
if e != nil {
   fmt.Println("Error : ",e)
}else{
   fmt.Println(n[0]["taskData"])
}
Run Code Online (Sandbox Code Playgroud)

得到这样的输出

map[owner_Uname:alice]
Run Code Online (Sandbox Code Playgroud)

我需要使用另一个查询访问这个结果字符串。这是一个界面,我尝试将其转换为简单的地图 newMap :=n[0]["taskData"].(map[string]interface{}),但它给了我一个运行时错误interface conversion: interface {} is bson.M, not map[string]interface {}

result := rawData{}
err := collection.Find(bson.M{"user_name":n[0]["taskData"]["owner_Uname"]}).All(&result)
Run Code Online (Sandbox Code Playgroud)

现在我想在上面的查询中使用它......请帮助我。提前致谢

编辑:- mongo 中的数据就像

{
   "_id" : ObjectId("56bf128f5a9a6a0ebfdd5075"),
     "deadLine" : {
       "Start_time" : ISODate("2016-05-24T00:00:00Z"),
       "End_time" : ISODate("2016-05-29T00:00:00Z")
     },
   },
   "taskData" : …
Run Code Online (Sandbox Code Playgroud)

struct go mongodb bson mgo

4
推荐指数
1
解决办法
7378
查看次数

标签 统计

bson ×1

go ×1

mgo ×1

mongodb ×1

struct ×1