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

后端 未结 1 1280
半阙折子戏
半阙折子戏 2021-01-05 11:43

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.NewDocum         


        
相关标签:
1条回答
  • 2021-01-05 12:12

    Edit: As the mongo-go driver evolved, it is possible to specify a projection using a simple bson.M like this:

    options.FindOne().SetProjection(bson.M{"_id": 0})
    

    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:

    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)
    
    0 讨论(0)
提交回复
热议问题