I am new to mongodb-go-driver. But i am stuck.
cursor, e := collection.Find(context.Background(), bson.NewDocument(bson.EC.String(\"name\", id)))
for cursor.Nex
Newer "github.com/mongodb/mongo-go-driver" expects object IDs defined as
type Application struct {
ID *primitive.ObjectID `json:"ID" bson:"_id,omitempty"`
}
This serializes into JSON "ID":"5c362f3fa2533bad3b6cf6f0"
and here is how you get the ID from insert result
if oid, ok := res.InsertedID.(primitive.ObjectID); ok {
app.ID = &oid
}
Convert from string
appID := "5c362f3fa2533bad3b6cf6f0"
id, err := primitive.ObjectIDFromHex(appID)
if err != nil {
return err
}
_, err = collection.DeleteOne(nil, bson.M{"_id": id})
Convert into string
str_id := objId.Hex()