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
752 stars 60 forks source link

Get All from a collection #37

Closed henri9813 closed 3 years ago

henri9813 commented 3 years ago

Describe the bug

Syntax is wrong

result := []Book{}

err := mgm.Coll(&Book{}).SimpleFind(&result, bson.M{"age": bson.M{operator.Gt: 24}})

Should be:

result := []Book

err := mgm.Coll(&Book{}).SimpleFind(&result, bson.M{"age": bson.M{operator.Gt: 24}})

Or maybe i miss something ?

Additionnaly, maybe add the FindAll method example:

    var result []Book

    err := collection.SimpleFind(&result, bson.D{})

    log.Fatalf("here is the books: %v", result)

best regards

mehran-prs commented 3 years ago

Golang does not support this syntax:

result := []Book

You should either create a new instance of a slice using make or declare it via literal.

result := make([]Book,0)
// or 
result2:=[]Book{}



FindAll example could be good but I think when developers learn how to search with query parameters, so they can guess how should they return all documents (empty query)

henri9813 commented 3 years ago

Hello,

I'm sorry, but golang support this syntax.

I'm currently using it

    var result []Book
    err := myCollection.SimpleFind(&result, bson.D{})

I agree with you for the documentation

henri9813 commented 3 years ago

Oh, wait, we don't do the same things.

In my case, i just declare the array without instanciate it.

You declare an empty array in your case.

henri9813 commented 3 years ago

My first message is wrong ...

I close the issue, sorry for the wasted time.