Trying to find by ID

47 views
Skip to first unread message

Doug Barrett

unread,
May 24, 2013, 1:37:29 PM5/24/13
to mgo-...@googlegroups.com
Hi, I'm trying to reference a document by it's ID.  This is the struct I have set up:

type Placement struct {
Id bson.ObjectId "_id.omitempty"
Title string "Title.omitempty"
Body string "Body.omitempty"
URL string "URL.omitempty"
Thumbnail string "Thumbnail.omitempty"
Paused bool "Paused"
Spread int "Spread.omitempty"
}

And when inserting data into MongoDB, this is the format I'm using:

newPlacement := Placement{
bson.NewObjectId(),
ctx.Params["Title"],
ctx.Params["Body"],
ctx.Params["URL"],
ctx.Params["Thumbnail"],
false,
spread,
}

That all seems to be working well, but I can't for the life of me load up a document by it's 'Id'.  This is the code I'm trying to use:
 
err := c.Find(bson.M{"Id" : bson.ObjectIdHex(id)}).One(&placement)
 
When that fires, I'm getting the error:  Handler crashed with error not found

I tried changing the code to:

err := c.Find(bson.M{"Id" : bson.ObjectIdHex(id)}).All(&placement)

But that's not returning any results, and I'm not getting an error.  I've looked at this thread: https://groups.google.com/forum/?fromgroups#!searchin/mgo-users/find$20by$20id/mgo-users/E3UGskD8oTc/2JJ-XeeNr8MJ but none of the methods bsr is using seems to work.

I'm pretty lost at this point, and am open to any suggestions.

Thank you!

Doug Barrett

unread,
May 24, 2013, 1:46:00 PM5/24/13
to mgo-...@googlegroups.com
I realized that I set the 'Id' to '_id' in MongoDB in the struct, so I assume that's what where MongoDB has saved the ID.  I changed how .Find() looks, and it still will not return any results.

Gustavo Niemeyer

unread,
May 24, 2013, 1:50:30 PM5/24/13
to mgo-...@googlegroups.com
On Fri, May 24, 2013 at 2:46 PM, Doug Barrett <doug.b....@gmail.com> wrote:
> I realized that I set the 'Id' to '_id' in MongoDB in the struct, so I
> assume that's what where MongoDB has saved the ID. I changed how .Find()
> looks, and it still will not return any results.

Please provide us with a tiny self-contained example, and we'll point
out what's happening.


gustavo @ http://niemeyer.net

Gustavo Niemeyer

unread,
May 24, 2013, 1:51:21 PM5/24/13
to mgo-...@googlegroups.com
On Fri, May 24, 2013 at 2:37 PM, Doug Barrett <doug.b....@gmail.com> wrote:
> Hi, I'm trying to reference a document by it's ID. This is the struct I
> have set up:
>
> type Placement struct {
> Id bson.ObjectId "_id.omitempty"

Actually, that's likely what's happening.

What separates the field name from the flags in the tag is a comma, not a dot.


gustavo @ http://niemeyer.net

Doug Barrett

unread,
May 24, 2013, 1:55:24 PM5/24/13
to mgo-...@googlegroups.com
Thank you very much! That was it Gustavo, I can't believe I didn't catch that.

Doug
Reply all
Reply to author
Forward
0 new messages