Rails3.2使用mongoDB学习笔记之mongoid

MongoDB  /  houtizong 发布于 2年前   180
前段时间写过一个mongo_mapper的demo程序,在写那个测试程序的时候,看见很多人都说mongoid更好,于是打算用一下试试,写了好久了,最近公司比较忙,没时间整理,现在贴出来。

新建项目
rails new spec_mongoid


一、mongonid
   官方首页http://mongoid.org/
  1、安装gem
    Gemfile中添加安装
    gem "mongoid"    gem "bson_ext"

  2、配置
    执行命令:
    $ rails g mongoid:config

    会在系统中生成一个mongoid的配置文件
    create  config/mongoid.yml


    如果出现 Could not find a JavaScript runtime.这个错误请在Gemfile中添加
    gem 'execjs'    gem 'therubyracer'

    修改config/application.rb文件,注释
    # require 'rails/all'

    用下面几行取而代之
    require "action_controller/railtie"    require "action_mailer/railtie"     require "active_resource/railtie"    require "rails/test_unit/railtie"

   
    把文件environment/development.rb和environment/test.rb中注释掉
    # config.active_record.mass_assignment_sanitizer = :strict     # config.active_record.auto_explain_threshold_in_seconds = 0.5 


    删除database.yml这个文件

    记得加载
    Mongoid.load!("path/to/your/mongoid.yml")


    日志application.rb中添加
    config.mongoid.logger = Logger.new($stdout, :warn)

    关闭日志的方法是在mongoid的配置文件中
     logger: false


  3、语言
    创建config/initializers/mongoid.rb
    添加代码
    Mongoid.add_language("zh-CN")

    如果是支持所有语言的话
    Mongoid.add_language("*")



=======================
问题
我用的全部都是最新版本,绝对的小白鼠阿,遇到了如下恶心问题
host is not a valid option for Mongo::Connection

官方说这是一个bug,需要重新安装这个gem
修改
gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'

但是弄完了之后,又出现这个
undefined method `add_language' for Mongoid:Module (NoMethodError)

只好把mongo.rb中的 
#Mongoid.add_language("*")
注释掉
=======================

$ rails g model user      invoke  mongoid      create    app/models/user.rb      invoke    test_unit      create      test/unit/user_test.rb      create      test/fixtures/users.yml


上面的命令在执行之后,给user.rb自动加上Mongoid的引用
   class User     include Mongoid::Document      end

随后运行rails console,测试了几个代码
ruby-1.9.2-p180 :003 > User.create(:name=>"李磊") => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: nil, age: nil>ruby-1.9.2-p180 :002 > user = User.find('4f630bf1421aa97127000001') => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: nil, age: nil> ruby-1.9.2-p180 :004 > user.update_attributes(:name=>"李磊",:login=>"lilei",:age=>15) => true ruby-1.9.2-p180 :005 > user => #<User _id: 4f630bf1421aa97127000001, _type: nil, name: "李磊", login: "lilei", age: 15> 


总体还算是成功

写个单元测试试一试
先把test_helper.rb中的下面代码注释掉
  # fixtures :all

然后在user_test.rb中添加:
# encoding: utf-8require 'test_helper'class UserTest < ActiveSupport::TestCase  test "create a User" do    assert_difference "User.count",1 do      User.create(:name=>"李磊",:age=>15,:login=>"lilei")    end     lilei = User.last    assert_equal [lilei.name,lilei.age,lilei.login],["李磊",15,"lilei"]  end end

运行单元测试:
rake test:unitsStarted.Finished in 0.020769 seconds.1 tests, 2 assertions, 0 failures, 0 errors, 0 skips


总结:
除了那个新版本不稳定的bug之外,总体上比mongomaper还是简单一些,需要配置的东西很少
但是官网的说明文档是在是不敢恭维,导航的体验太差了,尤其是上下级的那个色调。



实例代码地址
https://github.com/chinacheng/spec_mongoid

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客