新建项目

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/.html
create public/.html
create public/.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 stylesheets
gem '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: false
end

运行bundle install

bundle install
Updating git://github.com/mongoid/mongoid.git
Fetching gem metadata from https://rubygems.org/...........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.)
Using i18n (0.6.)
Using minitest (4.7.)
Using multi_json (1.8.)
Using atomic (1.1.)
Using thread_safe (0.1.)
Using tzinfo (0.3.)
Using activesupport (4.0.)
Using builder (3.1.)
Using erubis (2.7.)
Using rack (1.5.)
Using rack-test (0.6.)
Using actionpack (4.0.)
Using mime-types (1.25.)
Using polyglot (0.3.)
Using treetop (1.4.)
Using mail (2.5.)
Using actionmailer (4.0.)
Using activemodel (4.0.)
Using activerecord-deprecated_finders (1.0.)
Using arel (4.0.)
Using activerecord (4.0.)
Using bson (2.0.)
Using bson_ext (1.5.)
Using bundler (1.5.)
Using coffee-script-source (1.6.)
Using execjs (2.0.)
Using coffee-script (2.2.)
Using thor (0.18.)
Using railties (4.0.)
Using coffee-rails (4.0.)
Using connection_pool (1.2.)
Using hike (1.2.)
Using jbuilder (1.5.)
Using jquery-rails (3.0.)
Using json (1.8.)
Using mongo (1.3.)
Using optionable (0.2.)
Using moped (2.0..beta4)
Using origin (2.0.)
Using mongoid (4.0..alpha1) from git://github.com/mongoid/mongoid.git (at master)
Using tilt (1.4.)
Using sprockets (2.10.)
Using sprockets-rails (2.0.)
Using rails (4.0.)
Using rdoc (3.12.)
Using sass (3.2.)
Using sass-rails (4.0.)
Using sdoc (0.3.)
Using turbolinks (2.1.)
Using uglifier (2.4.)
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 的文件内容

 development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: mongoid_app_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:
options:
# Change the default write concern. (default = { w: })
# write:
# w: # Change the default consistency model to primary, secondary.
# 'secondary' will send reads to secondaries, 'primary' sends everything
# to master. (default: primary)
# read: secondary_preferred # How many times Moped should attempt to retry an operation after
# failure. (default: )
# max_retries: # The time in seconds that Moped should wait before retrying an
# operation on failure. (default: )
# retry_interval:
# Configure Mongoid specific options. (optional)
options:
# Enable the identity map, needed for eager loading. (default: false)
# identity_map_enabled: false # Includes the root model name in json serialization. (default: false)
# include_root_in_json: false # Include the _type field in serializaion. (default: false)
# include_type_for_serialization: false # Preload all models in development, needed when models use
# inheritance. (default: false)
# preload_models: false # Protect id and type from mass assignment. (default: true)
# protect_sensitive_fields: true # Raise an error when performing a #find and the document is not found.
# (default: true)
# raise_not_found_error: true # Raise an error when defining a scope with the same name as an
# existing method. (default: false)
# scope_overwrite_exception: false # Use Active Support's time zone in conversions. (default: true)
# use_activesupport_time_zone: true # Ensure all times are UTC in the app side. (default: false)
# use_utc: false
test:
sessions:
default:
database: mongoid_app_test
hosts:
- localhost:
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries:
retry_interval:

使用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

 class User
include Mongoid::Document
field :name, type: String
field :email, type: String
end
运行测试服务器
rails server
=> Booting WEBrick
=> Rails 4.0. application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[-- ::] INFO WEBrick 1.3.
[-- ::] INFO ruby 2.0. (--) [x86_64-linux]
[-- ::] INFO WEBrick::HTTPServer#start: pid= port=

访问地址http://localhost:3000/users 并添加一条数据

运行mongo查看数据是否添加成功

mongo
MongoDB shell version: 2.4.
connecting to: test
> show dbs
mongoid_app_development .203125GB
test (empty)
> use mongoid_app_development
switched to db mongoid_app_development
> show collections
system.indexes
users
> db.users.find()
{ "_id" : ObjectId("52c1330a75627524e2000000"), "name" : "grj", "email" : "grj@example.com" }
> exit
bye

下载: mongoid_app.rar

rails 4.0.2 + mongoid 对mongodb进行增删改查的更多相关文章

  1. Java连接MongoDB进行增删改查

    1.导入必须的包: 详情看项目:http://pan.baidu.com/s/1cvDAOY 2.通过Myeclipse创建WEB项目 3. 3.bean:创建实体类 package com.bean ...

  2. Java实现mongodb原生增删改查语句

    Java实现mongodb原生增删改查语句 2018-03-16 自动化测试时,需校验数据库数据,为了快速自动化,在代码中用原生增删改查语句操作mongodb 结构 代码 0 pom.xml < ...

  3. Scala对MongoDB的增删改查操作

    =========================================== 原文链接: Scala对MongoDB的增删改查操作 转载请注明出处! ==================== ...

  4. C# 对MongoDB 进行增删改查的简单操作

    C# 对MongoDB 进行增删改查的简单操作   下面演示下C#操作MongoDB驱动的简单的增删改查代码 运用到的MongoDB支持的C#驱动,当前版本为1.6.0 1,连接数据库   /// & ...

  5. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  6. python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)

    一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...

  7. 2,MongoDB之增删改查及pymongo的使用

    本章我们来学习一下关于 MongoDB的增删改查 一.MongoDB操作 之 原生ORM,根本不存在SQL语句 创建数据库:这里和一般的关系型数据库一样,都要先建立一个自己的数据库空间 是的,Mong ...

  8. MongoDB的增删改查 转

    MongoDB的增删改查 (黎明你好原创作品,转载请注明) MongoDB中数据的基本单元叫做文档,采用json的键-值的方式.多个键及其关联的值有序的存放在一起变是文档.类似于编程语言中的键值关系. ...

  9. SpringBoot操作MongoDB实现增删改查

    本篇博客主讲如何使用SpringBoot操作MongoDB. SpringBoot操作MongoDB实现增删改查 (1)pom.xml引入依赖 <dependency> <group ...

随机推荐

  1. HGE引擎 - 绘制,声音,碰撞处理

    原帖地址:http://blog.csdn.net/i_dovelemon/article/details/8818037 另外,年代久远,该引擎官网早已上不去了!!! 1.库的安装和下载 从官网上h ...

  2. a标签中调用js的几种方法

    1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题,而且javascript:协议 ...

  3. 学习OpenCV第0天

    自2011年接触OpenCV已经有几年了,一直停留在写一些小程序,利用手冊完毕一些任务,一直没有深入研究当中代码,现在毕业,但各种原因未能进入图像处理行业,故现重学OpenCV,包含分析代码,学习算法 ...

  4. Automatically generate serial number in abap

    流水号的产生:   在ABAP开发中,很多地方都需要按照自己的规则生成流水号,把这些流水号保存进透明表,作为唯一标示. 一. 系统中设定流水号 使用T-Code SNRO来创建一个流水号标识对象. 输 ...

  5. The JSR-133 Cookbook for Compiler Writers(an unofficial guide to implementing the new JMM)

    The JSR-133 Cookbook for Compiler Writers by Doug Lea, with help from members of the JMM mailing lis ...

  6. Java 中泛型的全面解析(转)

    Java泛型(generics) 是JDK 5中引入的一个新特性,允许在定义类和接口的时候使用类型参数(type parameter).声明的类型参数在使用时用具体的类型来替换.泛型最主要的应用是在J ...

  7. The practice program of C on point

    //字符反向排列 //vision 1.2 #include<stdio.h> void reverse_string( char *str ) { char *string;//第一个字 ...

  8. 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张

    自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var  定义变量,let定义常量. 比如 ...

  9. android 网络运营商的名字显示规则(锁定屏幕,下拉列表)

    一:Background & 有关flow MTK Operator name display分为两种类型的手机: 1. Sim卡名称: 从基于引导SIM卡读取IMSI到Spn-conf.xm ...

  10. 学习FFmpeg API – 解码视频

    本文转载 视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的 ...