Install MongoDB
Download:
http://www.mongodb.org/downloads
Extract the files to a directory(e.g, /opt/mongodb)
Create data directory:
$ sudo mkdir -p /data/db
Start MongoDB server:
$ sudo /opt/mongodb/bin/mongod
Start shell and connection to MongoDB server for test:
$ /opt/mongodb/bin/mongo
> db.foo.save( { a : 1} )
> db.foo.find()
> exit
Install Rails3 and mongo_mapper
sudo gem install rails
sudo gem install mongo_mapper
sudo gem install bson_ext
Create Rails project that use MongoDB
Create project:
$ rails new MongoDBTest --skip-active-record
Edit the Gemfile:
gem 'rails', '3.0.3'
gem 'mongo_mapper'
gem 'bson_ext'
Create config/initializers/mongo.rb:
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "MongoDBTest-production"
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect_to_master if forked
end
end
Create a model app/models/user.rb:
class User
include MongoMapper:Document
key :name
end
Start Rails Console for test:
$ rails console production
>> User.create(:name => "User A")
=> #<User name: "User A", _id: BSON::ObjectId('4d01c70d98d1b1072b000001')>
>> User.create(:name => "User B")
=> #<User name: "User B", _id: BSON::ObjectId('4d01c70f98d1b1072b000002')>
>> User.all
=> [#<User name: "User A", _id: BSON::ObjectId('4d01c70d98d1b1072b000001')>, #<User name: "User B", _id: BSON::ObjectId('4d01c70f98d1b1072b000002')>]
分享到:
相关推荐
ruby on rails对mongodb的操作ruby on rails对mongodb的操作ruby on rails对mongodb的操作ruby on rails对mongodb的操作
The Rails™ 3 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 3. Pioneering Rails expert Obie Fernandez and a team of leading experts illuminate ...
The Rails™ 3 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 3. Pioneering Rails expert Obie Fernandez and a team of leading experts illuminate ...
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
学习MongoDB API 端点 User#create (创建一个新用户) curl -v -H "Content-type: application/json" -X POST http://localhost:3000/api/v1/users -d '{"user":{"name":"nitin misra", "email":"nitin@example....
MongoModel - Ruby ODM用于与MongoDB数据库交互
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
2.我们将要使用MongoMapper来驱动MongoDB到Rails 编辑GemFile,增加下面的内容 gemmongo_mapper 然后 执行 bundle install 安装gem bundle install 3.添加数据库链接 在config/initializer下面新建一个mongo.rb...
#特征从任何全长网址创建唯一的缩短网址不重复已经创建的短网址从主页导航到短网址跟踪每个短网址的访问次数#设置Rails v 4.1.4 Ruby v 2.1.2 mongodb v 2.6.5(brew 安装 mongodb) 捆绑安装,运行 mongod,然后...
Windows7+Cygwin+Git+RVM+Ruby1.9.3+Rails3+MongoDB+N ginx+Unicorn+Rspec+Guard+Spork(1-Cygwin 安装配置)
rails指南 中文版
RUBY的经典之作,对其在RAILS下开发写得很详细
Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf
introduction for rails 4 development, it is very good document.
rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发
Ruby on Rails 4.0 Guide 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
学习Ruby on Rails 4.0的逐步指南。 它包括针对Ruby 2.0.0的基本教程,是为至少了解另一种编程语言并熟悉HTML的程序员编写的。
Rails3教程系列 DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY...
Each recipe has been updated to reflect the latest features of Rails 3 and each lays out a distinctive solution to a problem you may be facing today or could well encounter tomorrow. In addition, you...