Closed micahlmartin closed 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)
I'll close this issue. Please feel free to reopen it if needed.
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?
Here are the results:
Any idea what I'm doing wrong?