A
A
Artem Prokhorov2022-02-13 14:16:09
MongoDB
Artem Prokhorov, 2022-02-13 14:16:09

How to make FindOne with a selection of specific fields using the driver's office?

I want something like this:

var user bson.M
users.FindOne(ctx, bson.M{"username": "some"}).SetProjection(bson.M{"username": 1}).Decode(&user)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gill-sama, 2022-02-13
@kotcich

use the appropriate structure:

type usernameOnly struct {
  username string`bson:"username"`
}
var user usernameOnly
users...Decode(&user)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question