Can someone post a commented code of a basic use of MongoDB. initialization and queries. If there is smth else to know I'm all ears.
评论:
jayposs:
erdeicodrut:Here's a basic example:
https://play.golang.org/p/MrI29X_DjpYou will need to study both the MongoDB and MGO docs to really learn how to use the more sophisticated features.
The type bson.M is an alias for map[string]interface{}
jayposs:I'm really thankfull. it seems to be working. If im not asking for too much, can you give an example of inserting data into the db?
erdeicodrut:aPerson := person_doc{LastName:"bower", Zip:"71234"} // _id is auto loaded if not set err = collection.Insert(aPerson)
if mgo.IsDup(err) {
// got a duplicate key error, shouldn't happen with auto loaded id }
// update
err = collection.UpdateId(docId, bson.M{"$set": bson.M{"last_name":"bauer"}}Hopefully that works.
I usually set the key value using: doc.Id = bson.NewObjectId().Hex()
That way I know what the key value is up front and it is a simple string that is easy to work with.
PaluMacil:Thank you
I've only used Mongo in C#, but goinbigdata has an example that looks pretty good. It uses Goji, but if you wanted a different router, that part shouldn't be hard to swap out since it is composable with net/http.
