Hi,
I’m working on a simple CRUD application. The db I’m using is MongoDB, and there’s a problem when it comes to using collection.FindOne, to extract a document from my collection. I can’t get the actual values of the two fields I’ve suffixed with “ID”, namely orderID, and itemID. They store simple integer values like 1, 2, 3, 4 etc. Every time I use FIndOne, I get the correct document, with all the correct values except orderID and itemID. They’re both displayed as 0, for every document. Can someone please help me with this? Thanks.
Hi Arnav,
It’s been a while since I’ve used MongoDB, but if I remember correctly you need to use something called bson
.
It comes as part of the Go MongoDB Driver.
Alternatively, it can be used as a standalone package.
There’s a really good tutorial on the MongoDB website for using MongoDB and BSON with Go.
I hope this was helpful
Thank you for the quick reply. But I’ve been following the same adapter and tutorial. But nothing solves this issue. Even using bson/primitive to pass the correct type, but the issue still persists. Even reading the ObjectID returns ObjectID(“0000000…”), instead of the alphanumeric string seen in MongoDB Compass. Kinda weird.
Hm, that’s strange. So it’s correct in the stored document though right? It’s just went it is read that it doesn’t work?
Edit: Can you provide an example of the code you’re using to read the document?
It’s correct in the actual DB, but what I’m getting in the result is “all zeroes”.
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Person struct {
ID primitive.ObjectID
Name string
Age int
}
func main() {
// router setup
r := mux.NewRouter()
// MongoDB initialization
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB")
collection := client.Database("people").Collection("general")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var result Person
err := collection.FindOne(context.TODO(), bson.M{}).Decode(&result)
if err != nil {
log.Fatal(err)
}
// json.NewEncoder(w).Encode(result)
fmt.Println(result)
}).Methods("GET")
http.ListenAndServe("0.0.0.0:8888", r)
}
Output: {ObjectID("000000000000000000000000") Arnav 24}
Actual ObjectID: "5d26190f6f297fc7ef229ea3"
This is an absolute shot in the dark, but try changing bson.M{}
to bson.D{{}}
and see if the result is the same?
This is incorrect, please see the next comment.
Now I’m getting the right result, thanks. bson.M{} works fine. But I’ve noticed that any name which has “id” or “ID” in the end, isn’t read. Eg: pid is read correctly, but pID isn’t. Are there some rules that I’m missing.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.