douxiyi2418 2016-02-15 12:15
浏览 64
已采纳

如何使用Go Map从Mongo检索非结构化数据形式

I'm trying to retrieve data using go map. The data in mongo is like

"_id" : ObjectId("56bf128f5a9a6a0ebfdd5075"),
    "deadLine" : {
      "Start_time" : ISODate("2016-05-24T00:00:00Z"),
      "End_time" : ISODate("2016-05-29T00:00:00Z")
    },
    "taskData" : {
      "Task_content" : "Something",
      "Priority" : "3"
    },
    "group" : {
      "1" : {
      "grp_name" : "grp"
      },
      "2" : {
      "grp_name" : "secondGrp"
      }
    }

And I want to retrieve all records according to Priority.

the sample code which i tried ...

var m []bson.M
    err := collection.Find(bson.M{"taskData":bson.M{"Priority" : "2"}}).All(&m) // stuck here in `Find()`
    if err != nil {
        fmt.Println("Error : ",err)
    }else{
      fmt.Println("Map : ",m)
    }
  }

If i use err := collection.Find(bson.M{"_id":bson.ObjectIdHex("56bf128f5a9a6a0ebfdd5075")}).All(&m) then it fetch all records. Kindly correct my mistake.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doutuo3575 2016-02-15 13:05
    关注
    var m []bson.M
        err := collection.Find(bson.M{"taskData.Priority" : "2"}).All(&m) 
        if err != nil {
            fmt.Println("Error : ",err)
        }else{
          fmt.Println("Map : ",m)
        }
      }
    

    Here is the description https://docs.mongodb.org/manual/tutorial/query-documents/#equality-match-on-fields-within-an-embedded-document

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改