mongo_mapper&ruby

Mongomapper 用户mongo数据库配置,以及其提供了很多mongo的方法,可以很好地用来 对mongo相关封装和操作。

环境准备:gem install mongo_mapper

和activerecord很类似。将mongo的collection(表)和 类一一对应。

mongo_mapper 网上方法资源:http://mongomapper.com/documentation/plugins/querying.html

1、mongo配置从yml文件中读取

mongo55:
host: ***
port: ***
database: ****

2、mongo collection封装:

1)初始化mongo数据库连接

2)将需要的collections name 以类形式封装

#encoding:utf-8
require 'active_model'
require 'mongo'
require 'mongo_mapper'

=begin
author: kanlijun
date: 2016/07/13
description: 使用mongo_mapper模块实现collections封装,统一调配;初始化mongo连接,定义表和类对应关系
=end
class ConnectMongo
def initialize(host,port,database)
MongoMapper.connection =Mongo::Connection.new(host,port)
MongoMapper.database=database
end
end
#mobile
class Mobile
include MongoMapper::Document

set_collection_name 'mobile'
end

#ncontacts
class Ncontact
include MongoMapper::Document

end

3、调用rb文件案例:

1)读取yml配置数据,并初始化;

2)使用mongo_mapper的mongo操作方法

#encoding:utf-8
require 'mongo_mapper'
require_relative '../source/core/mongodb'

DATA_PATH='../source/data/mongo.yml'
mongo = YAML.load(File.open(DATA_PATH))
db =mongo['mongo55']
host,port,database =db['host'],db['port'],db['database']
ConnectMongo.new(host,port,database)

mobile = Mobile.all(:anion=>'789757')
puts mobile[0]['de']
puts mobile.length

#条件搜索排序
nsms = Nsms.where(:urr=>'789757').sort(:time.desc).all
puts nsms[1]['p']
lim =Nsms.first
# puts lim
puts lim['urr']

#排序
mo= Nsms.sort(:time.desc).first
puts mo['urr']

#随机拿出几个
mo= Nsms.limit(3).all
puts mo[0]['urr']
puts mo[2]['urr']

count = Nsms.where(:urr=>'789757').count

puts "count:#{count}" 

ruby&mongo_mapper的更多相关文章

  1. 安装cocoapods遇到两大坑-Ruby版本升级和Podfile的配置

    今天安装cocoapods #移除原有ruby源 $ gem sources --remove https://rubygems.org/ #使用可用的淘宝网 $ gem sources -a htt ...

  2. Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

    安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...

  3. 安装了ruby后怎么安装sass

    在命令行中输入 ruby -v 查看版本号 先移除默认的https://rubygems.org源,命令为gem sources --remove https://rubygems.org/,按回车 ...

  4. ruby 基础知识(一)

    突然今天发现一大神的博客:http://www.cnblogs.com/jackluo/archive/2013/01/22/2871655.html    相信初学者会受益颇多 ruby  参考文档 ...

  5. ruby 基础知识(二)

    ruby  中的动态方法 http://singleant.iteye.com/blog/1680382 Rails 大量使用了符号(symbol).符号看上去很像变量名,不过以冒号作为前缀.符号的例 ...

  6. Ruby安装Scss

    Ruby安装Scss 引言 已经许久不写HTML了,今天有点以前的东西要改.但是刚装的Windows10,已经没有以前的Web开发环境了.只好重新安装. 结果Webstorm装好后配置Scss出现错误 ...

  7. fzf by ruby

    fzf by ruby */--> fzf by ruby 1 github地址 https://github.com/junegunn/fzf 2 简介 软件通过匿名管道和grep扩展了bas ...

  8. The Safe Navigation Operator (&.) in Ruby

    The most interesting addition to Ruby 2.3.0 is the Safe Navigation Operator(&.). A similar opera ...

  9. Ruby on Rails 创建https应用

    1. 创建证书请求文件条件:私钥+证书签名请求+opensslyum install -y opensslmkdir /root/ssl/ && cd /root/ssl/openss ...

随机推荐

  1. Linux 安装图形界面及远程连接

    #可查询哪些组件是否已经安装(可用来对照组件名称) yum grouplist yum groupinstall 'X Window System' -y #安装GNOME桌面环境 yum group ...

  2. alter table的用法

    在数据库开发过程中,除了用得最多的数据库查询外,我们有时也需要去修改数据表的定义,比如在已存在的数据表中新增列和删除列等.这篇文章就总结一下alter table语句的用法. 示例代码如下. USE ...

  3. yii2.0高级框架配置时打开init.bat秒退的解决方法 (两种方法)

    第一种: 这几天刚接触到yii2.0框架,在配置advanced版本时运行init.bat初始化文件时老是闪退: 用cmd运行该文件时显示:The OpenSSL PHP extension is r ...

  4. hdoj 3501

    Problem Description Given a positive integer N, your task is to calculate the sum of the positive in ...

  5. 关于MediaPlayer的详细介绍

    1)如何获得MediaPlayer实例:可以使用直接new的方式:MediaPlayer mp = new MediaPlayer();也可以使用create的方式,如:MediaPlayer mp ...

  6. HDU 2871 Memory Control

    一共4种操作 其中用线段树 区间合并,来维护连续空的长度,和找出那个位置.其他用vector维护即可 #include<cstring> #include<cstdio> #i ...

  7. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  8. Wince 6.0 窗口最大化显示

    在InitDialog用如下代码实现: CRect   m_FullScreenRect;   //全屏区域 CRect   WindowRect; GetWindowRect(&Window ...

  9. Linux kernel map

  10. iOS 10 UserNotifications 框架解析

    摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...