LoginSignup
7
9

More than 5 years have passed since last update.

続!ElixirでMongoDBに接続するだけの簡単なお仕事です!!

Last updated at Posted at 2015-06-04

タイトル: 続!ElixirでMongoDBに接続するだけの簡単なお仕事です!!

目的: ElixirからMongoDBのドライバを使ってCRUD操作する。

目次:

1.概要
2.ElixirからCRUD操作を行う

実行環境:

OS: Windows8.1
Erlang: Eshell V6.4
Elixir: v1.0.4
elixir-mongo: v0.5.1
MongoDB:v3.0.3

1. 概要

タイトルと目的の通り。
ElixirからMongoDBのドライバを利用しCRUD操作を行います。

sapporo.beamに参加させて頂きました。
niku様に教えて頂いた参考リンクのお陰様でCRUD操作ができました。
http://sapporo-beam.github.io/

実行のための準備等は以前の記事を参考にして下さい。
ElixirでMongoDBに接続するだけの簡単なお仕事です!!

2. ElixirからCRUD操作を行う

mongo_test.ex
mongo = Mongo.connect!
db = mongo |> Mongo.db("phoenix_bbs")
collection = db |> Mongo.Db.collection("comments")

# init data
Mongo.Collection.drop collection
[
  %{name: "hoge", title: "hoge", comment: "hogehoge"},
  %{name: "huge", title: "huge", comment: "hugehuge"},
  %{name: "darui", title: "blogger", comment: "darui"}
] |> Mongo.Collection.insert(collection)

IO.puts "insert result"
%{name: "foo", title: "bar", comment: "foobar"} |> Mongo.Collection.insert_one!(collection)

# find
docs = collection |> Mongo.Collection.find |> Enum.to_list
IO.inspect docs

IO.puts "update result"
collection |> Mongo.Collection.update(%{name: "foo"}, %{name: "fizz", title: "bazz", comment: "fizzbazz"})

# find
docs = collection |> Mongo.Collection.find |> Enum.to_list
IO.inspect docs

IO.puts "delete result"
collection |> Mongo.Collection.delete(%{name: "fizz"})

# find
docs = collection |> Mongo.Collection.find |> Enum.to_list
IO.inspect docs

Mongo.Server.close(mongo)

実行結果

insert result
[%{_id: ObjectId(55703854f328cfab505c6d99), comment: "hogehoge", name: "hoge",
   title: "hoge"},
 %{_id: ObjectId(55703854f328cfab505c6d9a), comment: "hugehuge", name: "huge",
   title: "huge"},
 %{_id: ObjectId(55703854f328cfab505c6d9b), comment: "darui", name: "darui",
   title: "blogger"},
 %{_id: ObjectId(55703854f328cfab505c6d9c), comment: "foobar", name: "foo",
   title: "bar"}]

update result
[%{_id: ObjectId(55703854f328cfab505c6d99), comment: "hogehoge", name: "hoge",
   title: "hoge"},
 %{_id: ObjectId(55703854f328cfab505c6d9a), comment: "hugehuge", name: "huge",
   title: "huge"},
 %{_id: ObjectId(55703854f328cfab505c6d9b), comment: "darui", name: "darui",
   title: "blogger"},
 %{_id: ObjectId(55703854f328cfab505c6d9c), comment: "fizzbazz", name: "fizz",
   title: "bazz"}]

delete result
[%{_id: ObjectId(55703854f328cfab505c6d99), comment: "hogehoge", name: "hoge",
   title: "hoge"},
 %{_id: ObjectId(55703854f328cfab505c6d9a), comment: "hugehuge", name: "huge",
   title: "huge"},
 %{_id: ObjectId(55703854f328cfab505c6d9b), comment: "darui", name: "darui",
   title: "blogger"}]

前回の記事からまったく進歩していないべた書きですね。
その内、モジュールにしたり関数化したら修正します。

to_listしない状態でIO.inspectすると、以下のような表示結果になる。

delete result
%Mongo.Find{batchSize: 0,
 collection: %Mongo.Collection{db: %Mongo.Db{auth: nil,
   mongo: %Mongo.Server{host: '127.0.0.1', id_prefix: 6253, mode: :passive,
    opts: %{}, port: 27017, socket: #Port<0.5607>, timeout: 6000},
   name: "phoenix_bbs", opts: %{mode: :passive, timeout: 6000}},
  name: "comments", opts: %{}}, mods: %{}, mongo: nil, opts: %{},
 projector: %{}, selector: %{}, skip: 0}

以上!!

以下、参考にさせて頂いたサイト様
https://github.com/checkiz/elixir-mongo/blob/master/test/mongo_crud_test.exs
sapporo.beam#66参加者の方々

7
9
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
7
9