How can I filter fields with the mongo-go-driver. Tried it with findopt.Projection but no success.
type fields struct {
_id int16
}
s := bson.NewDocument()
filter := bson.NewDocument(bson.EC.ObjectID("_id", starterId))
var opts []findopt.One
opts = append(opts, findopt.Projection(fields{
_id: 0,
}))
staCon.collection.FindOne(nil, filter, opts...).Decode(s)
In the end, I want to suppress the field "_id". But the documents didn't change.
Edit: As the mongo-go driver evolved, it is possible to specify a projection using a simple
bson.M
like this:Original (old) answer follows.
The reason why it doesn't work for you is because the field
fields._id
is unexported, and as such, no other package can access it (only the declaring package).You must use a field name that is exported (starts with an uppercase latter), e.g.
ID
, and use struct tags to map it to the MongoDB_id
field like this:And now to perform a query using a projection:
Note that you may also use a
bson.Document
as the projection, you don't need your own struct type. E.g. the following does the same: