mongo.Unmarshal not quite right

17 views
Skip to first unread message

Hokapoka

unread,
Dec 13, 2010, 9:40:05 AM12/13/10
to golang-nuts
Hey there.

I see there's been some activity on the gomongo package from mikejs
but it appears that the mongo.Unmarshal fails to populate properties
of the Unmarshaled object that are arrays/slices.

Take the following structure as an example :

type Example struct {
Prop1 string
Prop2 string
Prop3 []string
}

If I were to insert this into the document :

doc := &Example{
Prop1 : "Value 1",
Prop2 : "Value 2",
Prop3 : []string{
"Value 3 a",
"Value 3 b",
"Value 3 c",
"Value 3 d",
},
}

docIn, _ := mongo.Marshal(doc)
coll.Insert(docIn)

The document is inserted into mongodb, the values of Prop3 are stored
correctly, and can be extracted with other mongodb drivers.

Then using any of the Find methods it successfully locates and returns
the document that can then be Unmarshalled using :

docOut := coll.FindOne(query)
var theDoc Example
mongo.Unmarshal( docOut.Bytes(), &theDoc)

This appears to unmarshal the bson data successfully, as the
properties theDoc.Prop1 & theDoc.Prop2 contain their respective
values, whereas theDoc.Prop3, the []string, has len(theDoc.Prop3)
equal 0.

I have tried to fix the issue but without much success, I was wonder
if anyone has managed todo this as yet?

It's a real shame that it doesn't work ATM, as the ability to store
arrays of detailed within a document in the database is one of
mongobds greatest features, IMHO.

Of course I could get around it by using some other structure and
that's what I'm doing, just wondered if there was a patch avaliable.

Many Thanks.






Hokapoka

unread,
Dec 13, 2010, 11:24:06 AM12/13/10
to golang-nuts
Having looked at the Parse func that is called when an object is
unmarshalled it appears that something odd is happening.

Here : https://github.com/mikejs/gomongo/blob/master/mongo/bson.go#L439

For each iteration through the Parse method I'm added
fmt.Println( "Processing : " + name + cval ) to the end.

The variable cval is only is an empty string unless the kind that is
being processed is StringKind, if this is the case cval has the
following assigned to it :

cval = " -- String value : " + string(s)


The following document has been inserted into the

doc := &Example{
Prop1 : "Value 1",
Prop2 : "Value 2",
Prop3 : []string{
"Value 3 a",
"Value 3 b",
"Value 3 c",
"Value 3 d",
},
}

This is what's rendered when the object is Unmarshalled :

Processing : id_
Processing : prop1 -- String value : Value 1
Processing : prop2 -- String value : Value 2
Processing : 0 -- String value : Value 3 a
Processing : 1 -- String value : Value 3 b
Processing : 2 -- String value : Value 3 c
Processing : 3 -- String value : Value 3 d
Processing : prop3 -- String value : Value 2
Processing : prop1 -- String value : Value 1
Processing : prop2 -- String value : Value 2
Processing : 0 -- String value : Value 3 a
Processing : 1 -- String value : Value 3 b
Processing : 2 -- String value : Value 3 c
Processing : 3 -- String value : Value 3 d
Processing : prop3 -- String value : Value 2
Processing : id_ -- String value : Value 2

I don't quite understand why it runs through all of the properties
twice.

However, that aside, there appears to be a clear issue with prop3, the
[]string. It appears to think it's a StringKind and it's assigning
the string "Value2" to it.

Although it's supposed to contain the collection of strings, that you
can see are indeed being extracted with their correct indexes.

Daniel Smith

unread,
Dec 13, 2010, 11:38:48 AM12/13/10
to Hokapoka, golang-nuts
I don't know if I fixed this bug or not, but I fixed a similar one a while back. I put up the fork of gomongo that I'm using here if you want to look:

It's probably quite far out of sync with the main repository.

Hokapoka

unread,
Dec 13, 2010, 11:51:39 AM12/13/10
to golang-nuts
Hey Daniel

Right now I'm banging my head against the desk!

why? ... because I had read your post on here about the fix you had
made, so I gave it ago, but when I tried it it failed to return the
object.

I just re-tried again, and noticed that you're not converting the the
keys to lower case, which I actually kinda like.

Simple change to the Find and hey presto, it does indeed work.

And the Array does Unmarshal as expected.

Many many thanks, much appreciated.

Daniel Smith

unread,
Dec 13, 2010, 12:20:13 PM12/13/10
to Hokapoka, golang-nuts
Glad it worked :)

Sorry, I forgot I changed that or I would have said something!
Reply all
Reply to author
Forward
0 new messages