我正在使用mgo在golang中进行mongo查询,以查询子元素以返回整个元素
{
"_id" : ObjectId("5b64a0d3931653c36bcaf0b5"),
"quantity" : 2,
"product" : "ABC",
"children" : [
{
"isBlocked" : true,
"blockedMessage" : "Error occurred: TRACEID",
"serialNo" : "abc123",
"token" : "foo456",
}
]
}
我在以下bson.M {“ _ id”:0,“ children”:bson.M {“ $ elemMatch”:{serialNo:'abc123'}}}中使用的查询
Find(MongoSpec{Selector: bson.M{}, Query: bson.M{"_id": 0, "children": bson.M{"$elemMatch": fields}}})
下面是查找功能
documents := []interface{}{}
s := spec.(MongoSpec).Selector
q := spec.(MongoSpec).Query
query := session.
DB(repo.Config.DatabaseName).
C(repo.CollectionName).
Find(s)
if q != nil {
query = query.Select(q)
}
err := query.All(&documents)
MongoSpec结构
type MongoSpec struct {
Selector interface{}
Query interface{}
}
上面的查询工作正常,但仅返回如下子元素
"children" : [
{
"isBlocked" : true,
"blockedMessage" : "Error occurred: TRACEID",
"serialNo" : "abc123",
"token" : "foo456",
}
]
我没有得到查询有什么问题。
慕标5832272
相关分类