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脚手架生成测试程序
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