mongodb - How to get Mongo documents and return them as a JSON API response -
i'm writing api retrieves mongo documents , return documents json response.
i can creating struct proper field mappings, since i'm not processing these documents, want convert raw data code below json. api return json response.
i have following code:
var raw []bson.raw err = mycollection.find( bson.m{"name": name}, ).all(&raw) i want convert raw json. how that? there better of this other starting create bson.raw?
tech stack: go 1.1 mgo v1 http://godoc.org/labix.org/v1/mgo bson v1 http://godoc.org/labix.org/v1/mgo/bson thanks.
unmarshal maps instead:
var maps []bson.m err = mycollection.find(bson.m{"name": name}).all(&maps) this way can provide these same maps encoding/json package's marshal function.
Comments
Post a Comment