{"id":6167,"date":"2011-10-02T04:55:32","date_gmt":"2011-10-01T19:55:32","guid":{"rendered":"http:\/\/fukata.org\/?p=6167"},"modified":"2017-08-11T22:58:49","modified_gmt":"2017-08-11T22:58:49","slug":"golang-go-mongo-basic-usage","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/6167\/","title":{"rendered":"go-mongo\u3067\u306e\u57fa\u672c\u64cd\u4f5c"},"content":{"rendered":"
\u4e00\u90e8\u306e\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306bgolang\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u3001DB\u306fMongoDB\u3060\u3063\u305f\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n
garyburd\/go-mongo – GitHub<\/a><\/p>\n \u3082\u3046\u4e00\u3064\u3001gomongo<\/a>\uff08\u540d\u524d\u304c\u7d1b\u3089\u308f\u3057\u3044\uff09\u3068\u3044\u3046\u306e\u3082\u3042\u308b\u305d\u3046\u3067\u3059\u304c\u3001\u3053\u3063\u3061\u306f\u73fe\u30ea\u30ea\u30fc\u30b9\u7248\u306egolang\u3060\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u901a\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u3001\u4e0a\u8a18\u306e\u3082\u306e\u3092\u5229\u7528\u3057\u307e\u3057\u305f\u3002<\/p>\n github\u306eREADME\u306b\u60c5\u5831\u304c\u5c11\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u3068\u3057\u3066\u57fa\u672c\u7684\u306a\u64cd\u4f5c\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a18\u8f09\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n \u76ee\u6b21<\/p> go-mongo\u3067\u306fBSON\u304b\u3089golang\u5185\u306e\u578b\u5909\u63db\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n \u5f8c\u3067master\u30d6\u30e9\u30f3\u30c1\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u898b\u3066\u307f\u308b\u3068\u4e0a\u8a18\u306e\u65b9\u6cd5\u3067\u306f\u306a\u304f\u3001Query\u306b\u5bfe\u3057\u3066Update\u3084Remove\u30e1\u30bd\u30c3\u30c9\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u305f\u306e\u3067\u3001\u4eca\u5f8c\u306f\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30a4\u30f3\u3067\u691c\u7d22\u6761\u4ef6\u3092\u69cb\u7bc9\u3057\u305f\u5f8c\u306b\u3001\u64cd\u4f5c\u304c\u884c\u3048\u308b\u3088\u3046\u306b\u3082\u306a\u308b\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n \u3053\u308c\u306f\u3001goinstall\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u308b\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u306f\u307e\u3060\u542b\u307e\u308c\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u4e00\u90e8\u306e\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306bgolang\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u3001DB\u306fMongoDB\u3060\u3063\u305f\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 garyburd\/go-mongo – GitHub \u3082\u3046 … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[909,1088],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6167"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=6167"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6167\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=6167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=6167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=6167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u57fa\u672c\u64cd\u4f5c<\/span><\/h2>\n
\u63a5\u7d9a\u3001\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u9078\u629e\u3001\u5207\u65ad<\/span><\/h3>\n
\r\n\/\/ \u63a5\u7d9a\r\nconn, err := mongo.Dial(\"localhost:27017\")\r\n\/\/ \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u9078\u629e\r\nc := mongo.Collection{conn, \"hoge.foocollections\", mongo.DefaultLastErrorCmd}\r\n\/\/ \u5207\u65ad\r\nconn.Close()\r\n<\/pre>\n
SELECT<\/span><\/h3>\n
\r\n\/\/ \u5358\u6570\r\n\/\/ \u5024\u3092\u30de\u30c3\u30d7\u306b\u683c\u7d0d\r\nvar foo_map mongo.M\r\nerr := c.Find(nil).One(&foo_map)\r\nlog.Println(foo_map[\"_id\"])\r\n\r\n\/\/ \u5024\u3092\u69cb\u9020\u4f53\u306b\u683c\u7d0d\r\ntype Foo struct {\r\n Id mongo.ObjectId `bson:\"_id\"`\r\n Name string `bson:\"name\"`\r\n}\r\n\r\nvar foo Foo\r\nerr := c.Find(nil).One(&foo)\r\nlog.Println(foo.Id)\r\n\r\n\/\/ \u8907\u6570\r\ncursor, err := c.Find(nil).Limit(10).Cursor()\r\nfor cursor.HasNext() {\r\n var f Foo\r\n err = cursor.Next(&f)\r\n log.Println(f)\r\n}\r\n<\/pre>\n
INSERT<\/span><\/h3>\n
\r\nid := mongo.NewObjectId()\r\nerr = c.Insert(&Foo{Id: id, Name: \"Tatsuya Fukata\"})\r\n<\/pre>\n
UPDATE<\/span><\/h3>\n
\r\n\/\/ \u5f15\u6570\uff1a \u691c\u7d22\u6761\u4ef6, \u66f4\u65b0\u60c5\u5831\r\nerr = c.Update(mongo.M{\"name\": \"Tatsuya Fukata\"}, mongo.M{\"name\": \"\u6df1\u7530\u9054\u4e5f\"})\r\n<\/pre>\n
DELETE<\/span><\/h3>\n
\r\nerr = c.Remove(mongo.M{\"name\": \"\u6df1\u7530\u9054\u4e5f\"})\r\n<\/pre>\n
BSON\u3068Golang\u306e\u5909\u63db\u8868<\/span><\/h2>\n
\r\n\/\/ BSON -> Go\r\n\/\/ Integer32 -> signed and unsigned integers, floats, bool\r\n\/\/ Integer64 -> signed and unsigned integers, floats, bool\r\n\/\/ Array -> []interface{}, other slice types\r\n\/\/ Binary -> []byte\r\n\/\/ Boolean -> bool\r\n\/\/ Datetime -> mongo.Datetime, int64\r\n\/\/ Document -> map[string]interface{}, struct types\r\n\/\/ Double -> signed and unsigned integers, floats, bool\r\n\/\/ MinValue, MaxValue -> mongo.MinMax\r\n\/\/ ObjectID -> mongo.ObjectId\r\n\/\/ Symbol -> mongo.Symbol, string\r\n\/\/ Timestamp -> mongo.Timestamp, int64\r\n\/\/ string -> string\r\n<\/pre>\n