我注意到,我可以将投影设置为不返回username
或userID
个字符串,而不会出现问题.但是,当try 不返回deactivated
或admin
布尔值时,即使其他字符串不返回,它们仍然会出现?
user := model.User{}
filter := bson.D{
{
Key: "_id",
Value: userID,
},
}
projection := bson.D{
{
Key: "password",
Value: 0,
},
{
Key: "username",
Value: 0,
},
{
Key: "_id",
Value: 0,
},
{
Key: "deactivated",
Value: 0,
},
{
Key: "admin",
Value: 0,
},
}
options := options.FindOne().SetProjection(projection)
_ = usersCol.FindOne(ctx, filter, options).Decode(&user)
&{ false false [0xc00028d180] 0xc0002b0900 0xc0002bdce0 <nil>}
type User struct {
ID string `json:"id" bson:"_id"`
Admin bool `json:"admin"`
Deactivated bool `json:"deactivated"`
Username string `json:"username"`
Password string `json:"password"`
...
}