Skip to content

Instantly share code, notes, and snippets.

@darui00kara
Created June 4, 2015 12:23
Show Gist options
  • Save darui00kara/93c9b1bd648bf4e9806f to your computer and use it in GitHub Desktop.
Save darui00kara/93c9b1bd648bf4e9806f to your computer and use it in GitHub Desktop.
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 ""
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 ""
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment