deserializing bson arrays

3,184 views
Skip to first unread message

dave...@gmail.com

unread,
Jan 27, 2013, 12:34:09 AM1/27/13
to mgo-...@googlegroups.com
I need to get user documents from MongoDB and unmarshall them into a struct defined like so

type user struct {
    email string
    password string
    loginHistory []bson.M{"timestamp": time.Time}
}

This fails to compile but I've tried many other things. What is the best way to do this? I've been trying to retrieve user documents like this

usr := user{}
err := users.Find(bson.M{"email":email, "password":password}).One(&usr)

This works when I leave on the loginHistory field but fails when I keep it. Thanks for the help.

Dave

Dave Cheney

unread,
Jan 27, 2013, 12:51:36 AM1/27/13
to mgo-...@googlegroups.com, mgo-...@googlegroups.com
Do the fields in your struct need to be exported?
--
 
 

dave...@gmail.com

unread,
Jan 27, 2013, 12:56:38 AM1/27/13
to mgo-...@googlegroups.com
Yes, I'll fix that. But it still doesn't explain why the array isn't being unmarshaled. Thanks.

dave...@gmail.com

unread,
Jan 27, 2013, 1:08:21 AM1/27/13
to mgo-...@googlegroups.com
Maybe it would work if I used nested structs like this?

type lh struct {
    Timestamp time.Time
}

type user struct {
    Email string
    Password string
    LoginHistory []lh
}

And just to clarify, one of my user documents in Mongo looks like this


{
  "_id" : ObjectId("5104be6dda9b1413f4967052"),
  "email" : "te...@test.com",
  "loginHistory" : [{
      "timestamp" : new Date("1/27/2013 00:45:11")
    }, {
      "timestamp" : new Date("1/27/2013 00:45:43")
    }, {
      "timestamp" : new Date("1/27/2013 00:47:11")
    }],
  "password" : "p"

Dave Cheney

unread,
Jan 27, 2013, 1:10:32 AM1/27/13
to mgo-...@googlegroups.com, mgo-...@googlegroups.com
You keen using unexported structures, I'm pretty sure that is the cause of your problem. 
--
 
 

dave...@gmail.com

unread,
Jan 27, 2013, 1:17:11 AM1/27/13
to mgo-...@googlegroups.com
My whole program is one package right now so I don't think it matters. The email and password fields are being unmarshalled just fine. The problem is the array of timestamps.
How can I deserialize this? It shouldn't be that hard should it?

Gustavo Niemeyer

unread,
Jan 27, 2013, 4:45:35 PM1/27/13
to mgo-...@googlegroups.com
Hi there,

On Sun, Jan 27, 2013 at 4:08 AM, <dave...@gmail.com> wrote:
> And just to clarify, one of my user documents in Mongo looks like this
>
>
> {
> "_id" : ObjectId("5104be6dda9b1413f4967052"),
> "email" : "te...@test.com",
> "loginHistory" : [{
> "timestamp" : new Date("1/27/2013 00:45:11")
> }, {
> "timestamp" : new Date("1/27/2013 00:45:43")
> }, {
> "timestamp" : new Date("1/27/2013 00:47:11")
> }],
> "password" : "p"
> }

You want types such as:

type Foo struct {
Id bson.ObjectId `bson:"_id"`
Email string
Password string
LoginHistory []LoginEntry `bson:"loginHistory"`
}

type LoginEntry struct {
Timestamp time.Time
}

gustavo @ http://niemeyer.net

dave...@gmail.com

unread,
Jan 27, 2013, 6:14:16 PM1/27/13
to mgo-...@googlegroups.com
You're awesome. Thanks!


On Sunday, January 27, 2013 12:34:09 AM UTC-5, dave...@gmail.com wrote:

wri...@ldlibra.com

unread,
Sep 7, 2013, 1:31:00 AM9/7/13
to mgo-...@googlegroups.com
I'm having the same problem as the OP, and am using the same solution you've provided, but I'm still getting an empty array back. Are there any other caveats known to make this work? Happy to post code if needed.

Gustavo Niemeyer

unread,
Sep 7, 2013, 1:35:26 AM9/7/13
to mgo-...@googlegroups.com

Sure, send it along and we'll have a look.

--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

wri...@ldlibra.com

unread,
Sep 7, 2013, 1:43:30 AM9/7/13
to mgo-...@googlegroups.com
Wow... After many hours of questing, I've realized I was querying a collection called 'sparx', when the desired collection was actually called 'sparxs'. Please forgive my wasting of your finger movements to respond *bows*. Have a grand eve!
Reply all
Reply to author
Forward
0 new messages