Reflect tag and mgo (mongo driver) - handle object id

332 views
Skip to first unread message

bsr

unread,
Jun 25, 2012, 11:39:25 PM6/25/12
to golan...@googlegroups.com
Hello,
I want to set (during query) a field (ID) of my struct to mongodb object id (_id). I also insert this struct in DB from xml. To avoid setting empty string as object id, I added the reflect tag `xml:"-"`. 

As seen below in both cases the results are not what I desire.

Field1: ID bson.ObjectId `xml:"-" "_id"`

{
   "_id": ObjectId("4fe92c4beaf9553e4d114af6"),
   "id": "",
   "headerlevel": {


Field 2: ID bson.ObjectId `xml:"-" bson:"_id"`
 {
   "_id": "",
   "headerlevel": {

Desired behavior is
1. mgo insert objectid while insert
2. Populate the ID field with _id during query.


thanks.

Gustavo Niemeyer

unread,
Jun 26, 2012, 1:16:08 AM6/26/12
to bsr, golan...@googlegroups.com
On Mon, Jun 25, 2012 at 8:39 PM, bsr <bsr...@gmail.com> wrote:
> As seen below in both cases the results are not what I desire.
>
> Field1: ID bson.ObjectId `xml:"-" "_id"`

I suspect you want this:

ID bson.ObjectId `bson:"_id,omitempty"`


gustavo @ http://niemeyer.net

bsr

unread,
Jun 26, 2012, 8:31:51 AM6/26/12
to golan...@googlegroups.com, bsr
Indeed.. thanks
Reply all
Reply to author
Forward
0 new messages