Find by id with mgo
You either use Collection.FindId()
and then you pass only the id value, or you use Collection.Find()
and then you have to specify a value with the field name too:
err2 := c.FindId(bson.ObjectIdHex("58593d1d6aace357b32bb3a1")).One(&data)
// OR
err2 := c.Find(bson.M{"_id": bson.ObjectIdHex("58593d1d6aace357b32bb3a1")}).
One(&data)
If you get no errors, that means the document is found.
If you always see 0
printed (as the value of the id_cookie.IdCookie
field), that means the field in the document holding this id has a different name.
Use struct tags to tell how it is stored in your MongoDB. E.g. if in your MongoDB it is called "myid"
, you can map it like this:
type id_cookie struct {
IdCookie int `bson:"myid"`
}
Also note that you should not connect to the MongoDB server every time you want to query some data, instead connect once, and just reuse the session. for details see: mgo - query performance seems consistently slow (500-650ms)
Bussiere
Updated on June 22, 2022Comments
-
Bussiere almost 2 years
I would like to find a data by
_id
. I know that this data exists and that this_id
exist (I've tested it with pymongo).But the code below doesn't find it:
type id_cookie struct { IdCookie int } func get_id_mongo() int { session, err := mgo.Dial("127.0.0.1") if err != nil { panic(err) } defer session.Close() // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) c := session.DB("id_bag").C("id_cookie") data := id_cookie{} err2 := c.FindId(bson.M{"_id": bson.ObjectIdHex("58593d1d6aace357b32bb3a1")}).One(&data) if (err2 != nil){ Info.Println("error") Info.Println(err2) } Info.Println(data) return data.IdCookie }
It just returns me a
0
.But I can find it using pytmongo and python.
import requests import pymongo from pymongo import MongoClient from bson.objectid import ObjectId from pprint import pprint client = MongoClient('127.0.0.1', 27017) import base64 db = client.id_bag pprint(db.collection_names()) result = db.id_cookie.insert_one( { 'IdCookie': 1 }) print(result.inserted_id) data = db.id_cookie.find_one({"_id": ObjectId("58593d1d6aace357b32bb3a1")}) print(data)
here are the result :
['id_cookie', 'system.indexes'] 58593d2d6aace357b32bb3a3 {'IdCookie': 1, '_id': ObjectId('58593d1d6aace357b32bb3a1')}
Does anyone have any idea?
Edit : i've try with :
err2 := c.FindId(bson.ObjectIdHex("58593d1d6aace357b32bb3a1")).One(&data)
but i still have 0 :
INFO: 2016/12/20 15:42:08 Cookie_Id.go:147: 1 INFO: 2016/12/20 15:42:08 Cookie_Id.go:149: 2 INFO: 2016/12/20 15:42:18 Cookie_Id.go:87: data INFO: 2016/12/20 15:42:18 Cookie_Id.go:88: {0} INFO: 2016/12/20 15:42:18 Cookie_Id.go:89: 0 INFO: 2016/12/20 15:42:18 Cookie_Id.go:118: 0 INFO: 2016/12/20 15:42:18 Cookie_Id.go:128: OK
-
Bussiere over 7 yearsThanks for the note but IdCookie is not _id
-
icza over 7 years@user462794 Please see edited answer. If you use
FindId()
, you only have to pass the id value (and not wrapped in abson.M
). Or useFind()
and then you can pass id wrapped inbson.M
. -
Bussiere over 7 yearsI still have 0 : INFO: 2016/12/20 15:42:08 Cookie_Id.go:147: 1 INFO: 2016/12/20 15:42:08 Cookie_Id.go:149: 2 INFO: 2016/12/20 15:42:18 Cookie_Id.go:87: data INFO: 2016/12/20 15:42:18 Cookie_Id.go:88: {0} INFO: 2016/12/20 15:42:18 Cookie_Id.go:89: 0 INFO: 2016/12/20 15:42:18 Cookie_Id.go:118: 0 INFO: 2016/12/20 15:42:18 Cookie_Id.go:128: OK
-
icza over 7 years@user462794 The code I posted works (verified). There must be something else you've not showed us and is different.
-
Bussiere over 7 yearsit was the : IdCookie int
bson:"IdCookie"
thanks a lot -
Mike over 6 yearsit does not work I am guessing something is wrong with the insert, I cannot select it via _id