I have problems with queries on UUID filed in my mongo collection. Mongo document structure is like:
{
"_id": {
"$oid": "5acf7faff5f02b0001e9fda1"
},
"j": {
"$uuid": "d0459793-3ec0-71fd-319e-b959af081db6"
},
"s": "ok",
"creation_date": {
"$date": "2018-04-12T15:47:59.003Z"
}
}
I want to get document, passing j
uuid (not ObjectID). I create mongo connection, and I get my collection, then I try to execute this query:
import (
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
...
var job *Job
uid, _ := uuid.FromString(m.ID)
e := c.Find(bson.M{"j": uid.String()}).One(&job)
but e
is always equal to "not found".
m.ID
is string of uuid without dashes-
so I convert it to uuid.UUID
.
Other queries like c.Find(bson.M{"s": "ok"}).All(&jobs)
work fine, so I'm sure about connection & collection.
Using golang 1.11 & mongodb 3.6.
UPDATE:
When I do db.mycol.find()
from mongo console result is a list of documents like:
{ "_id" : ObjectId("5acf5b0ac7fb0700010040ac"), "j" : BinData(3,"amOjUW1oQQ6dNsvLrQuDhg=="), "s" : "ok", "creation_date" : ISODate("2018-04-12T13:11:38.365Z") }
so I tried to modify my query like this:
e := c.Find(bson.M{"j": bson.Binary{0x03, []byte(m.ID)}}).One(&job)
And still it does not return the document.