I am working on a mongo query in golang using mgo to query a child element to return the entire element
{
"_id" : ObjectId("5b64a0d3931653c36bcaf0b5"),
"quantity" : 2,
"product" : "ABC",
"children" : [
{
"isBlocked" : true,
"blockedMessage" : "Error occurred: TRACEID",
"serialNo" : "abc123",
"token" : "foo456",
}
]
}
The query I am using in below 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}}})
Below is the find function
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 struct
type MongoSpec struct {
Selector interface{}
Query interface{}
}
The above query works fine but returns only children element as below
"children" : [
{
"isBlocked" : true,
"blockedMessage" : "Error occurred: TRACEID",
"serialNo" : "abc123",
"token" : "foo456",
}
]
I am not getting what is wrong with the query.