mongodb - mgo: Find fields of type number (int, float64) doesn't work -
i'm developing restful api in go mgo driver mongodb.
the problem i'm trying fetch documents field of type int , no results returned.
for example have document:
{ "_id" : objectid("5797833e9de6f8c5615a20f9"), "id" : "28743915-9be0-427d-980d-5009bfe1b13a", "name" : "hunter", "rating" : 2.9, "downloads" : 5040 }
and when trying fetch document with:
conn.session. db("face"). c("papers"). find(bson.m{"rating": 2.9}). all(&papers) // papers instance of slice struct.
it not return documents doing same in mongo shell returns documents, example:
db.papers.find({"rating": 2.9})
but doing in mongo shell won't return documents:
db.papers.find({"rating": "2.9"})
so think problem might when bson.m being serialized might convert value string, because bson.m map[string]interface{}
this how paper struct looks like
type paper struct { id string `json:"id,omitempty" bson:"id"` name string `json:"name,omitempty" bson:"name"` rating float64 `json:"rating" bson:"rating"` downloads int `json:"downloads" bson:"downloads"` }
i'm trying understand problem , can't. confusing part problem explanation
i'm trying fetch documents field of type int
find(bson.m{"rating": 2.9}).
"rating" : 2.9
in world 2.9
integer?
- there no type
number
(which have mentioned in title) nor in go, nor in mongo. - if inserting
float64
mgo
it's inserted mongodouble
- you can't query
double
string
query.db.papers.find({"rating": 2.9})
returns documents ,db.papers.find({"rating": "2.9"})
- not, if there few documents"rating": 2.9
.
there no error in mgo
nor in mongo
. if interested, here sample code inserts map , retrieves data paper
class:
package main import ( "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "fmt" ) type paper struct { id string `json:"id,omitempty" bson:"id"` name string `json:"name,omitempty" bson:"name"` rating float64 `json:"rating" bson:"rating"` downloads int `json:"downloads" bson:"downloads"` } func main() { session, err := mgo.dial("mongodb://127.0.0.1:27017/face") if err != nil { panic(err) } defer session.close() session.setmode(mgo.monotonic, true) c := session.db("face").c("papers") c.insert(bson.m{ "id": "28743915-9be0-427d-980d-5009bfe1b13a", "name": "hunter", "rating": 2.9, "downloads": 5040, }) var papers []paper c.find(bson.m{"rating": 2.9}).all(&papers) // equivalent of db.papers.find({"rating": 2.9}) fmt.printf("%+v\n", papers) }
Comments
Post a Comment