文章标题
2016-03-23 11:47
591 查看
需要的环境:
安装:mongodb
gem包: mongo(ruby driver)
安装:mongodb
gem包: mongo(ruby driver)
require 'mongo' Mongo::Logger.logger.level = ::Logger::FATAL (去掉控制台的log) #连接mongodb,选择一个数据库(可以不存在,在插入数据之后,可以在库中show dbs 看到被创建的db) db = Mongo::Client.new(["localhost:27017"],:database=>"runoob") #创建一个索引col并插入一条数据 要用json的数据形式 res = db["col"].insert_one({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) res = db["col"] #选择新建的索引col res.find.each do |x| #find方法可以返回索引中的文档 puts x["title"] end #创建一个索引artists,并插入多条数据,json的数据形式 res = db[:artists].insert_many([ {title:'art1',content:"today is a good day 1",create_at:Time.now.strftime("%F %T")}, {title:'art2',content:"today is a good day 2",create_at:Time.now.strftime("%F %T")}, {title:'art3',content:"today is a good day 3",create_at:Time.now.strftime("%F %T")}, {title:'art4',content:"today is a good day 4",create_at:Time.now.strftime("%F %T")}, {title:'art5',content:"today is a good day 5",create_at:Time.now.strftime("%F %T")}, {title:'art6',content:"today is a good day 6",create_at:Time.now.strftime("%F %T")}, ]) res = db[:artists].find.each do |x| puts x["content"] end
相关文章推荐
- 团队问题
- 168.View the Exhibit and examine the description of SALES and PROMOTIONS tables.
- java多线程回顾3:线程安全
- 使用 AForge.NET 做视频采集
- Ubuntu中root用户和user用户的相互切换
- CString 到 float 的转换 小方法
- c++运算符重载
- iOS关于数组的那些小菜菜
- Css3之高级-1 Css复杂选择器(兄弟选择器 、属性选择器、伪类选择器、伪元素选择器)
- 生产者和消费者关系通过多线程来实现
- 转换时间为字符串
- 解决ora-02429:无法用于删除强制唯一/主键的索引
- Android三级目录、ListView单选/GridView单选、ListView多选/GridView多选
- KVO,NSNotification、Block、Delegate和KVO的区别
- 夜神安卓模拟器--虚拟神器
- 2016-03-23对于课程的一些凌乱的想法
- 获取顶层视图控制器UIViewController
- 配置Nginx多核CPU,worker_cpu_affinity使用方法和范例
- alembicTool_shaderEdit
- 多线程的停止方法