设为首页 收藏本站
查看: 1560|回复: 0

[经验分享] rails 4.0.2 + mongoid 对mongodb进行增删改查

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2013-12-31 09:01:10 | 显示全部楼层 |阅读模式
rails new mongoid_app --skip-active-record --skip-test-unit --skip-bundle
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
修改Gemfile

source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'gem 'rails', '4.0.2'gem 'bson_ext'gem 'mongo'gem 'mongoid', github: 'mongoid/mongoid'
# Use SCSS for stylesheetsgem 'sass-rails', '~> 4.0.0'gem 'uglifier', '>= 1.3.0'gem 'coffee-rails', '~> 4.0.0'gem 'jquery-rails'gem 'turbolinks'gem 'jbuilder', '~> 1.2'group :doc do  gem 'sdoc', require: falseend
运行bundle install

bundle installUpdating git://github.com/mongoid/mongoid.gitFetching gem metadata from https://rubygems.org/...........Fetching additional metadata from https://rubygems.org/..Resolving dependencies...Using rake (10.1.1)Using i18n (0.6.9)Using minitest (4.7.5)Using multi_json (1.8.2)Using atomic (1.1.14)Using thread_safe (0.1.3)Using tzinfo (0.3.38)Using activesupport (4.0.2)Using builder (3.1.4)Using erubis (2.7.0)Using rack (1.5.2)Using rack-test (0.6.2)Using actionpack (4.0.2)Using mime-types (1.25.1)Using polyglot (0.3.3)Using treetop (1.4.15)Using mail (2.5.4)Using actionmailer (4.0.2)Using activemodel (4.0.2)Using activerecord-deprecated_finders (1.0.3)Using arel (4.0.1)Using activerecord (4.0.2)Using bson (2.0.0)Using bson_ext (1.5.1)Using bundler (1.5.1)Using coffee-script-source (1.6.3)Using execjs (2.0.2)Using coffee-script (2.2.0)Using thor (0.18.1)Using railties (4.0.2)Using coffee-rails (4.0.1)Using connection_pool (1.2.0)Using hike (1.2.3)Using jbuilder (1.5.3)Using jquery-rails (3.0.4)Using json (1.8.1)Using mongo (1.3.1)Using optionable (0.2.0)Using moped (2.0.0.beta4)Using origin (2.0.0)Using mongoid (4.0.0.alpha1) from git://github.com/mongoid/mongoid.git (at master)Using tilt (1.4.1)Using sprockets (2.10.1)Using sprockets-rails (2.0.1)Using rails (4.0.2)Using rdoc (3.12.2)Using sass (3.2.13)Using sass-rails (4.0.1)Using sdoc (0.3.20)Using turbolinks (2.1.0)Using uglifier (2.4.0)Your bundle is complete!Use `bundle show [gemname]` to see where a bundled gem is installed.
生成mongoid数据库配置文件
rails generate mongoid:config      create  config/mongoid.yml在这里不对mongoid.yml进行任何更改(假设已安装MongoDB并以默认设置启动),顺便贴一下生成的mongoid.yml 的文件内容

1 development: 2   # Configure available database sessions. (required) 3   sessions: 4     # Defines the default session. (required) 5     default: 6       # Defines the name of the default database that Mongoid can connect to. 7       # (required). 8       database: mongoid_app_development 9       # Provides the hosts the default session can connect to. Must be an array10       # of host:port pairs. (required)11       hosts:12         - localhost:2701713       options:14         # Change the default write concern. (default = { w: 1 })15         # write:16         # w: 117 18         # Change the default consistency model to primary, secondary.19         # 'secondary' will send reads to secondaries, 'primary' sends everything20         # to master. (default: primary)21         # read: secondary_preferred22 23         # How many times Moped should attempt to retry an operation after24         # failure. (default: 30)25         # max_retries: 3026 27         # The time in seconds that Moped should wait before retrying an28         # operation on failure. (default: 1)29         # retry_interval: 130   # Configure Mongoid specific options. (optional)31   options:32     # Enable the identity map, needed for eager loading. (default: false)33     # identity_map_enabled: false34 35     # Includes the root model name in json serialization. (default: false)36     # include_root_in_json: false37 38     # Include the _type field in serializaion. (default: false)39     # include_type_for_serialization: false40 41     # Preload all models in development, needed when models use42     # inheritance. (default: false)43     # preload_models: false44 45     # Protect id and type from mass assignment. (default: true)46     # protect_sensitive_fields: true47 48     # Raise an error when performing a #find and the document is not found.49     # (default: true)50     # raise_not_found_error: true51 52     # Raise an error when defining a scope with the same name as an53     # existing method. (default: false)54     # scope_overwrite_exception: false55 56     # Use Active Support's time zone in conversions. (default: true)57     # use_activesupport_time_zone: true58 59     # Ensure all times are UTC in the app side. (default: false)60     # use_utc: false61 test:62   sessions:63     default:64       database: mongoid_app_test65       hosts:66         - localhost:2701767       options:68         read: primary69         # In the test environment we lower the retries and retry interval to70         # low amounts for fast failures.71         max_retries: 172         retry_interval: 0
使用scaffold脚手架生成测试程序

rails generate scaffold User name:string email:string      invoke  mongoid      create    app/models/user.rb      invoke  resource_route       route    resources :users      invoke  scaffold_controller      create    app/controllers/users_controller.rb      invoke    erb      create      app/views/users      create      app/views/users/index.html.erb      create      app/views/users/edit.html.erb      create      app/views/users/show.html.erb      create      app/views/users/new.html.erb      create      app/views/users/_form.html.erb      invoke    helper      create      app/helpers/users_helper.rb      invoke    jbuilder      create      app/views/users/index.json.jbuilder      create      app/views/users/show.json.jbuilder      invoke  assets      invoke    coffee      create      app/assets/javascripts/users.js.coffee      invoke    scss      create      app/assets/stylesheets/users.css.scss      invoke  scss      create    app/assets/stylesheets/scaffolds.css.scss
顺便也贴一下生成的model文件 user.rb

1 class User2   include Mongoid::Document3   field :name, type: String4   field :email, type: String5 end
运行测试服务器
rails server=> Booting WEBrick=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000=> Run `rails server -h` for more startup options=> Ctrl-C to shutdown server[2013-12-30 16:46:00] INFO  WEBrick 1.3.1[2013-12-30 16:46:00] INFO  ruby 2.0.0 (2013-06-27) [x86_64-linux][2013-12-30 16:46:00] INFO  WEBrick::HTTPServer#start: pid=9442 port=3000
访问地址http://localhost:3000/users 并添加一条数据
运行mongo查看数据是否添加成功

mongoMongoDB shell version: 2.4.6connecting to: test> show dbsmongoid_app_development    0.203125GBtest    (empty)> use mongoid_app_developmentswitched to db mongoid_app_development> show collectionssystem.indexesusers> db.users.find(){ "_id" : ObjectId("52c1330a75627524e2000000"), "name" : "grj", "email" : "grj@example.com" }> exitbye



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-12777-1-1.html 上篇帖子: MongoDB 模糊查询,及性能测试 下篇帖子: mongodb基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表