Mongo: 投影不影响布尔值

huangapple go评论79阅读模式
英文:

Mongo: projection not affecting booleans

问题

我注意到的一件事是,我可以设置投影,不返回我的usernameuserID字符串,没有问题。然而,当尝试不返回deactivatedadmin布尔值时,它们仍然出现,即使其他字符串不会出现。

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"`
    ...
}
英文:

Something I've noticed is that I can set the projection to not return my username or userID strings with out a problem. However, when trying to not return the deactivated or admin boolean, they still appear even when the other strings wont?

	user := model.User{}
	filter := bson.D{
		{
			Key:   &quot;_id&quot;,
			Value: userID,
		},
	}
	projection := bson.D{
		{
			Key:   &quot;password&quot;,
			Value: 0,
		},
		{
			Key:   &quot;username&quot;,
			Value: 0,
		},
		{
			Key:   &quot;_id&quot;,
			Value: 0,
		},
		{
			Key:   &quot;deactivated&quot;,
			Value: 0,
		},
		{
			Key:   &quot;admin&quot;,
			Value: 0,
		},
	}
	options := options.FindOne().SetProjection(projection)
	_ = usersCol.FindOne(ctx, filter, options).Decode(&amp;user)

&amp;{ false false [0xc00028d180] 0xc0002b0900 0xc0002bdce0 &lt;nil&gt;}

type User struct {
	ID           string          `json:&quot;id&quot; bson:&quot;_id&quot;`
	Admin        bool            `json:&quot;admin&quot;`
	Deactivated  bool            `json:&quot;deactivated&quot;`
	Username     string          `json:&quot;username&quot;`
	Password     string          `json:&quot;password&quot;`
    ...
}

答案1

得分: 1

你看到的值是结构字段的默认值。我建议你使用指向User字段的指针:

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"`
    ...
}

在这个例子中,这些值将是nil

英文:

The values you see it is default value of struct field. I suggest you to use pointer to the field of User:

type User struct {
    ID           string           `json:&quot;id&quot; bson:&quot;_id&quot;`
    Admin        *bool            `json:&quot;admin&quot;`
    Deactivated  *bool            `json:&quot;deactivated&quot;`
    Username     string           `json:&quot;username&quot;`
    Password     string           `json:&quot;password&quot;`
    ...
}

In this example values would be nil.

huangapple
  • 本文由 发表于 2022年4月15日 18:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71882918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定