I am currently receiving a mongo aggregate query from a payload:
[
{
"$match": {
"isComplete": true
"accountId": "foo123"
"startTime": "2017-03-06T23:07:21.262Z"
"$or": [ { userId: "bar123" }, { userId: "bar235" } ]
}
},
{
"$group": {
"_id": null,
"count": {
"$sum": 1
}
}
}
]
This is being stored as a map[string]interface{}
. The issue is the $match
clause can be any arbitrary query, which means it can contain ObjectIds and Dates. I have tried manually converting ids to bson.ObejectId
s and any Dates to time.Times
but the queries can become very complicated.
Does anyone know any good way of taking an arbitrary mongo query from a payload and converting it?
It seems like you need to unmarshaling to a []map[string]interface{}
with that array structure in the post.
With that being said you can do something like this (with the currect map[string]interface{}
type.
type Payload struct {
Message map[string]interface{} `json:"message"`
}
// If you are using it as a http HandleFunc
func (s *Server) ProcessPayload() {
PayloadHandler := func(w http.ResponseWriter, r *http.Request) {
incoming := r.FormValue("message")
if incoming != "" {
var payload = new(Payload)
json.Unmarshal([]byte(incoming), payload)
go payload.FromPayload()
}
}
http.HandleFunc("/payload", PayloadHandler)
}
func (p *Payload) FromPayload() {
match, ok := p.Message["$match"]
if !ok {
return
}
// Do your work on the $match object
}