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

  1. i'm trying fetch documents field of type int

  2. find(bson.m{"rating": 2.9}).

  3. "rating" : 2.9

in world 2.9 integer?

  1. there no type number (which have mentioned in title) nor in go, nor in mongo.
  2. if inserting float64 mgo it's inserted mongo double
  3. 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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -