1

I am having a strange (to me) an issue with golang mongodb connector. The current problem is that I have a db retrieval function that actually returns no errors, but the struct expected is just not there (null). Here is part of the code that will probably give some hints:

[library]

func (db *NoSqlConnection[T]) CreateRecoveryTable(p *player.Player[T]) error {

    updt := bson.M{"id": p.Id}
    _, err := db.db.Collection(recoveryTableName).InsertOne(context.TODO(), updt)

    if err != nil {
        return fmt.Errorf("failed to update recovery state ")
    }
    return nil
}

func (db *NoSqlConnection[T]) AddRecoveryRecord(p *player.Player[T], record *recovery.Recovery[T]) (int64, error) {
    opts := options.UpdateOne().SetUpsert(true)
    updt := bson.M{"$set": bson.M{"game_id": p.GID, "record": record}}
    _, err := db.db.Collection(recoveryTableName).UpdateOne(context.TODO(), bson.M{"id": p.Id}, updt, opts)
    if err != nil {
        return -1, fmt.Errorf("failed to update recovery state")
    }

    return 1, nil
}

func (db *NoSqlConnection[T]) GetPlayerRecoveryById(id T) (recovery.Recovery[T], error) {
    filter := bson.M{"id": id}
    res := recovery.Recovery[T]{}
    err := db.db.
        Collection(playersTableName).
        FindOne(context.TODO(), filter).
        Decode(&res)

    if err != nil {
        if err == mongo.ErrNoDocuments {
            return res, err
        }
        return res, err
    }
    return res, nil
}

The problematic function is GetPlayerRecoveryById(id T) Other functions work correctly (creating / updating the records in my atlas test db).

The recovery model is as:

type SpecializedID interface {
    int64 | uint64 | uuid.UUID | string
}
...

type Recovery[T models.SpecializedID] struct {
    Id     T   `json:"id" bson:"id"`
    Record any `json:"record" bson:"record"`
}

So when I try to retrieve the record I never get an error, as I expect to have, but always get a zerofill record. I've tried almost all printing families , but can't seem to understand why there is nothing inside the that struct. What I am missing here?

5
  • What constrains T models.SpecializedID? if I'm reading your program correctly, it means you could have instances of both Recovery { Id int64; Record any; } in the same keyspace as Recovery { Id UUID, Record: any; } - right? Commented Dec 20, 2025 at 23:09
  • its used as uint64 in my code, yes. At the time of usage you can't have both. So there is no miss matched types. Commented Dec 21, 2025 at 4:21
  • Also in my test scenarios the id is 1 which is shown as long in mongo db atlas UI and matches with the ID in the other record where it is taken. Commented Dec 21, 2025 at 4:30
  • I suggest you copy+paste the above code, but remove the [T] generic type parameters and instead hard-code it to use uint64 and see if that works first. Commented Dec 21, 2025 at 4:32
  • I can try that, even though I have another db Player in a similar fashion that have no issue with the Id, but has no any field, I have a feeling something with any is messing up. Commented Dec 21, 2025 at 4:43

1 Answer 1

1

Ok, apparently the issue is something that avoided my eyes:

I am reading from playersTable instead of recoveryTable and mongo db has no issue telling me that it's fine you have an id and you got nil data, I guess it's by design, but for some other reason even if I change the id to be something different from the other base (like rec_id) it still fails with no errors and returns nil.
Closing this, since it's a fix and an advice be very careful and watch closely.
P.S. AI could not spot it for me.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.