Kamva / mgm

Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)
Apache License 2.0
754 stars 63 forks source link

Writing but not reading data #72

Closed micahlmartin closed 2 years ago

micahlmartin commented 2 years ago

I have mongo running locally and went through the examples in the readme. I'm able to write to the database with no issues. When I look in the collections all the data is there and being written. For some reason though none of the query methods return any data. I've verified the ID's do exist in the database. Any ideas?

// Setup the mgm default config
err := mgm.SetDefaultConfig(nil, "mgm_lab", options.Client().ApplyURI("mongodb://localhost:27017"))
log.Println("mgm default config:", err)

book := NewBook("Pride and Prejudice", 345)

// Make sure to pass the model by reference (to update the model's "updated_at", "created_at" and "id" fields by mgm).
err = mgm.Coll(book).Create(book)
log.Println("mgm create:", err)

// Get the document's collection
book = &Book{}
coll := mgm.Coll(book)

// Find and decode the doc to a book model.
b1 := coll.FindByID(book.IDField, book)
log.Println("mgm find by id:", b1)

// Get the first doc of the collection
b := coll.First(bson.M{}, book)
log.Println("mgm first:", b)

// Get the first doc of a collection using a filter
b2 := coll.First(bson.M{"pages": 400}, book)
log.Println("mgm first:", b2)

Here are the results:

2022/07/03 10:19:31 mgm default config: <nil>
2022/07/03 10:19:32 mgm create: <nil>
2022/07/03 10:19:33 mgm created: {ObjectID("62c1a5742a9077712a8d6b5b")}
2022/07/03 10:19:47 mgm find by id: mongo: no documents in result
2022/07/03 10:19:48 mgm first: <nil>
2022/07/03 10:19:49 mgm first: mongo: no documents in result

Any idea what I'm doing wrong?

mehran-prs commented 2 years ago

Hi @micahlmartin

I've added reasons of your outputs as comments.

    fmt.Println((&Book{}).ID)
    // Setup the mgm default config
    err := mgm.SetDefaultConfig(nil, "mgm_lab2", options.Client().ApplyURI("mongodb://localhost:27017"))
    log.Println("mgm default config:", err)

    book := NewBook("Pride and Prejudice", 345)

    // Make sure to pass the model by reference (to update the model's "updated_at", "created_at" and "id" fields by mgm).
    err = mgm.Coll(book).Create(book)
    log.Println("mgm create:", err)

    // Get the document's collection
    book = &Book{}
    coll := mgm.Coll(book)

    // Find and decode the doc to a book model.
    // You have an empty book model instance, so it's ID value is a zero
    // value: ObjectID("000000000000000000000000"). so it'll fetches nothing
    // because can not find any with that ID value.
    b1 := coll.FindByID(book.IDField, book)
    log.Println("mgm find by id:", b1)

    // Get the first doc of the collection
    // This will fetches the document, in the next line you print the error,
    // and there's not any error, so prints nothing, I've added another line
    // print the book after that.
    b := coll.First(bson.M{}, book)
    log.Println("mgm first:", b)
    fmt.Println("the first book",book)

    // Get the first doc of a collection using a filter
    // Same as previous
    b2 := coll.First(bson.M{"pages": 400}, book)
    log.Println("mgm first:", b2)
    fmt.Println("the first book",book)
mehran-prs commented 2 years ago

I'll close this issue. Please feel free to reopen it if needed.