How to filter fields from a mongo document with the official mongo-go-driver

耗尽温柔 提交于 2020-02-15 08:36:08

问题


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.


回答1:


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:

type fields struct {
    ID int `bson:"_id"`
}

And now to perform a query using a projection:

projection := fields{
    ID: 0,
}
result := staCon.collection.FindOne(
    nil, filter, options.FindOne().SetProjection(projection)).Decode(s)

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:

projection := bson.NewDocument(
    bson.EC.Int32("_id", 0),
)
result := staCon.collection.FindOne(
    nil, filter, options.FindOne().SetProjection(projection)).Decode(s)


来源:https://stackoverflow.com/questions/53120116/how-to-filter-fields-from-a-mongo-document-with-the-official-mongo-go-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!