Howto; String to ObjectID

瀏覽次數:18 次
跳到第一則未讀訊息

sc28

未讀,
2015年2月3日 下午3:53:432015/2/3
收件者:mgo-...@googlegroups.com
I have a process that is inserting a struct into a table:

    type Info struct {
ID     bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
        ... other
    }

    data := &Info{}
    data.ID := bson.NewObjectID()
    ...
    // do the insert - all OK

    I now need to pass the ObjectId to another process that will extract the record and do other work - here's where I'm running into problems/misunderstanding something fundamental:

    cmd := exec.Command("./otherProcess" "-id=", data.ID.Hex())
    cmd.Start()

In the otherProcess, I'm doing this:

    recordId := bson.ObjectIdHex(*examID) // *examID is read (works) from the commandline arg "-id"
    session.DB("myDB").C("MyCollection").FindId(bson.M{"_id": recordId}).One(&result)

    No records are found (the ObjectId *is* in the mongo collection).  So the issue I'm having is how to properly pass the ObjectId as a String to another process, and properly convert the string to a bson.ObjectId for lookup.

TIA!



sc28

未讀,
2015年2月3日 下午4:16:322015/2/3
收件者:mgo-...@googlegroups.com
Ok, figured it out - I needed to do this:

     session.DB("myDB").C("MyCollection").FindId(bson.ObjectIdHex(*examID)).One(&result)

instead of creating a bson.M{"_id", ...} struct.
回覆所有人
回覆作者
轉寄
0 則新訊息