I am new to Go and React, both of which I am using for this mini project. Go is running a backend api using Mongodb.
I am fetching the user list from Mongo in Go, and then sending that to React, problem is Mongo is giving me all of the fields for the user (_id, password, and username), I only want username. I am not understanding how I can filter this and prevent all fields from being sent from Go to React.
JSON Output from Go API:
{ "Success": true, "Data": [ { "_id": "6205ac3d72c15c920a424608", "password": { "Subtype": 0, "Data": "removed for security" }, "username": "removed for security" }, { "_id": "6206b44afb8b044fdba76b8f", "password": { "Subtype": 0, "Data": "removed for security" }, "username": "removed for security" } ] }
Here is my Go code for the specified route:
// Route: Get Users, for getting a list of users
func RouteGetUsers(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Set content-type to JSON
w.Header().Set("Content-Type", "application/json")
type Response struct {
Success bool `json:"Success"`
Data []bson.M `json:"Data"`
}
// Load the env file
err := godotenv.Load("variables.env")
if err != nil {
log.Fatal("Error loading .env file")
}
// Get Mongo DB environment variable
uri := os.Getenv("MONGO_URI")
if uri == "" {
log.Fatal("You must set your 'MONGO_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
}
// Connect to Mongo Database
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
// Close the database connection at the end of the function
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
// Select the database name and collection name
coll := client.Database("go_project1").Collection("users")
// Query the database for the user list
cursor, err := coll.Find(context.TODO(), bson.D{})
// If no documents were found, send a response and return
if err == mongo.ErrNoDocuments {
fmt.Printf("No documents were found")
return
}
// Setup a variable for the database results
var results []bson.M
// Send all database results to results variable
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
// Setup a variable with the ResponseStandard struct
response := Response{
Success: true,
Data: results,
}
// Marshal into JSON
responseJson, err := json.Marshal(response)
if err != nil {
fmt.Println(err)
}
// Send success response to user in JSON
fmt.Fprintf(w, "%s\n", responseJson)
}
CodePudding user response:
Use the projection option:
opts := options.Find().SetProjection(bson.D{{"usrename", 1}})
cursor, err := coll.Find(context.TODO(), bson.D{}, opts)
Yet another approach: Declare a type with the fields you want:
type Data struct {
Username string `bson:"username" json:"username"`
}
Fetch to that type:
var data []Data
if err = cursor.All(context.TODO(), &data); err != nil { ...
Send response with that type:
var response = struct {
Success bool `json:"Success"`
Data []Data `json:"Data"`
}{
true,
data,
}
responseJson, err := json.Marshal(response)
...
A third approach: Filter the maps in the question:
for _, result := range results {
for k := range result {
if k != "username" {
delete(result, k)
}
}
}