mikejs / gomongo

Go driver for MongoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Find by Id

kamilc opened this issue · comments

Hi all,

Is there currently any way to select records by it's Id ?
I've tried creating query map with "id_": ""
as well as creating query type with field Id_ []byte, both with
no success...

Here is a simple func to abstract finding one record from db:

func Find(collname string, id string) mongo.BSON {
  db := GetDb()

  coll := db.GetCollection(collname)

  _q := map[string]string{"_id": id}

  q, er :=  mongo.Marshal(_q)

  if er != nil {

    panic(er.String())

  }
  ret, er := coll.FindOne(q)

  if er != nil {

    println(er.String())

    panic(er.String())

  }

  return ret

}

But, then after a call to FindOne, er.String() gives:
cursor failure

Of, course I've checked if record exists with mongo client
and so on..

So old that I'm closing it now