英文:
How to retrieve unstructured form of data from mongo using go map
问题
我正在尝试使用go map检索数据。Mongo中的数据如下所示:
"_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"
}
}
我想根据Priority
检索所有记录。
我尝试的示例代码如下:
var m []bson.M
err := collection.Find(bson.M{"taskData": bson.M{"Priority": "2"}}).All(&m) // 在`Find()`这里卡住了
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Map: ", m)
}
如果我使用
err := collection.Find(bson.M{"_id": bson.ObjectIdHex("56bf128f5a9a6a0ebfdd5075")}).All(&m)
那么它会获取所有记录。
请帮我纠正错误。
提前感谢。
英文:
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
得分: 0
以下是要翻译的内容:
var m []bson.M
err := collection.Find(bson.M{"taskData.Priority" : "2"}).All(&m)
if err != nil {
fmt.Println("错误:",err)
}else{
fmt.Println("映射:",m)
}
}
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论