LoginSignup
8
8

More than 5 years have passed since last update.

GoでMongoDBを使う初歩

Posted at

 GoでDBを使おうと思いMongoDBを使う方法を勉強したのでメモします。mongoDBのライブラリーとして使用したのはmgoを使っています。使うまでの手順を以下に示します。

1. MongoDBのインストール
 インストールはhomebrewを使います。homebrewのインストールは別途してください。

brew install mongodb

2. bzrのインストール
 mgoをインストール際に必要なライブラリーです。こちらもhomebrewでインストールします。xcode5.1ではエラーが出たので事前にhomebrewをupdateしておきます。

brew update
brew install bzr

 事前準備はこれで終了です。次に本体をインストールしていきます。
3. mgoのインストール
 こちらはhomebrewではなくてgogetを使います。homebrewのように簡単にできます。

go get labix.org/v2/mgo

4. サンプルダウンロードして実行
 サンプルはGoogle先生が教えてくれたこちらのものをコピペで使用しました。https://gist.github.com/border/3489566

5. サンプルの実行
コピペで作成したものをsample.goとして保存し実行します。

go run sample.go

 プロンプトにいかが出たら成功です。

Phone {ObjectIdHex("53493562e2c176e7e52e894a") Ale 2014-04-12 21:45:22.96 +0900 JST}
Results All: [{ObjectIdHex("53493562e2c176e7e52e894a") Ale +55 53 1234 4321 2014-04-12 21:45:22.96 +0900 JST}]
Results All: [{ObjectIdHex("53493562e2c176e7e52e894a") Ale +86 99 8888 7777 2014-04-12 21:45:22.967 +0900 JST}]

以上で完成です。
少しいじってみてからまた情報upします。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8