Created
May 25, 2015 08:41
-
-
Save darui00kara/b2acea4fe8eef9c39e37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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