Skip to content

Instantly share code, notes, and snippets.

@darui00kara
Created May 25, 2015 08:41
Show Gist options
  • Select an option

  • Save darui00kara/b2acea4fe8eef9c39e37 to your computer and use it in GitHub Desktop.

Select an option

Save darui00kara/b2acea4fe8eef9c39e37 to your computer and use it in GitHub Desktop.
# encoding: utf-8
# File Name: mongo_main.rb
# Create Day is 2015/05/25
# Last Update Day is 2015/05/25
# Require List
require 'mongo'
puts "MongoDB test!!"
puts "DB接続"
db = Mongo::Client.new(["127.0.0.1:27017"], :database => 'sinatra_bbs')
collection = db[:comments]
# CRUD操作例
# 作成(C)
puts "ドキュメント挿入(一つ)"
puts collection.insert_one({:name => "darui", :title => "blogger", :comment => "darui"})
# 取得(R)
puts "ドキュメント取得(全件)"
collection.find.each do |doc|
puts doc
end
# 更新(U)
# $~の部分は色々あるようだ・・・一覧が見つからない
puts "ドキュメント更新(複数)"
collection.find(:name => "darui").update_many("$set" => {:comment => "darui@apprentice"})
collection.find.each do |doc|
puts doc
end
# 削除(D)
puts "ドキュメント削除(複数)"
collection.find(:name => "darui").delete_many
collection.find.each do |doc|
puts doc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment