how to directly insert a json string into a collection?

6,393 views
Skip to first unread message

Daniel Mal

unread,
Sep 17, 2013, 12:26:51 AM9/17/13
to mgo-...@googlegroups.com
how to directly insert a json string into a collection?

Gustavo Niemeyer

unread,
Sep 17, 2013, 12:40:23 AM9/17/13
to mgo-...@googlegroups.com
On Tue, Sep 17, 2013 at 1:26 AM, Daniel Mal <daniel...@gmail.com> wrote:
> how to directly insert a json string into a collection?

If you mean having a JSON string as a field in the database, you just do:

err := collection.Insert(bson.M{"yourField": yourJSONString})

If you actually mean storing the JSON string as a MongoDB document,
then you have to decode it first:

var m map[string]interface{}
err := json.Unmarshal([]byte(yourJSONString), &m)
if err != nil {
return err
}
err = collection.Insert(m)


gustavo @ http://niemeyer.net

Daniel Mal

unread,
Sep 17, 2013, 1:01:33 AM9/17/13
to mgo-...@googlegroups.com

yes, i mean the second situation.thanks. it works.

but, why the fields in struct saved with mgo are all lowercases, but when 'directly save with json', i got capitalized fields?

is it still unmarshalable into a struct?

在 2013年9月17日星期二UTC+8下午12时40分23秒,Gustavo Niemeyer写道:

Gustavo Niemeyer

unread,
Sep 17, 2013, 3:06:33 PM9/17/13
to mgo-...@googlegroups.com
On Tue, Sep 17, 2013 at 2:01 AM, Daniel Mal <daniel...@gmail.com> wrote:
> but, why the fields in struct saved with mgo are all lowercases, but when
> 'directly save with json', i got capitalized fields?

When mgo marshals a struct, it lowercases the fields by default
because that's by far the most common convention used with the MongoDB
database. If you want to use other casing, that's possible via the
`bson:"name"` field tag.

When you use a map, mgo won't touch the keys, though.

> is it still unmarshalable into a struct?

Yes, that's certainly fine.


gustavo @ http://niemeyer.net

Peter Kleiweg

unread,
Nov 17, 2013, 12:33:17 PM11/17/13
to mgo-...@googlegroups.com
Op dinsdag 17 september 2013 06:40:23 UTC+2 schreef Gustavo Niemeyer:
If you actually mean storing the JSON string as a MongoDB document,
then you have to decode it first:

    var m map[string]interface{}
    err := json.Unmarshal([]byte(yourJSONString), &m)
    if err != nil {
        return err
    }
    err = collection.Insert(m)

Why is this? I have a very large collection of json objects, all stored as strings in a text file. So I have to decode each string into a json object, and then mgo encodes this object back into a string and sends it to the server. This extra decoding/encoding seems wasteful.

Gustavo Niemeyer

unread,
Nov 18, 2013, 7:37:23 AM11/18/13
to mgo-...@googlegroups.com
No, you can simply store the json as strings in MongoDB too. Of
course, you won't be able to query them with all the benefits of
having data properly structured in MongoDB.
> --
> 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.



--

gustavo @ http://niemeyer.net

Peter Kleiweg

unread,
Nov 18, 2013, 11:38:28 AM11/18/13
to mgo-...@googlegroups.com
I tried. I get an error: Can't marshal string as a BSON document

Op maandag 18 november 2013 13:37:23 UTC+1 schreef Gustavo Niemeyer:

Islan Dberry

unread,
Nov 18, 2013, 11:49:20 AM11/18/13
to mgo-...@googlegroups.com
MongoDB collections contain documents. A document is a sequence of name-value pairs.

A string cannot be directly stored in a collection because a string is not a document.

You can store the string a field in a document.

Gustavo Niemeyer

unread,
Nov 18, 2013, 11:56:24 AM11/18/13
to mgo-...@googlegroups.com
This works, for example:

err := collection.Insert(bson.M{"json": jsonString})

Peter Kleiweg

unread,
Nov 18, 2013, 1:22:31 PM11/18/13
to mgo-...@googlegroups.com
Op maandag 18 november 2013 17:56:24 UTC+1 schreef Gustavo Niemeyer:

This works, for example:

    err := collection.Insert(bson.M{"json": jsonString})

That is not what I mean. I meant sending json "source code" to mongo. 

My idea was that mongo reads json as strings, but I was mistaken. Even the C driver works with bson objects, not "source strings".

Islan Dberry

unread,
Nov 18, 2013, 2:34:39 PM11/18/13
to mgo-...@googlegroups.com
On Monday, November 18, 2013 10:22:31 AM UTC-8, Peter Kleiweg wrote:
My idea was that mongo reads json as strings

The MongoDB protocol transmits documents as BSON (http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/). The server does not know about JSON.


Reply all
Reply to author
Forward
0 new messages