zoukankan      html  css  js  c++  java
  • 使用MongoDB ruby驱动进行简单连接/CRUD/运行命令

    1. 连接数据库

    require 'mongo'
    
    #access database 'test'
    client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
    db = client.database
    
    db.collections  #return a list of collection objects
    db.collections.name #return a list of collection names
    
    collection = client[:restaurant] #access collection 'restaurant'

    2. 插入文档

    a. 插入单个文档

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    
    collection = client[:people] #acess collection 'people'
    
    #insert a single document into the collection
    doc = { name: 'Steve', hobbie: ['hiking', 'tennis', 'fly fishing'] }
    result = collection.insert_one(doc)
    result.n #returns 1

    b. 插入多个文档

    require 'mongo'
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    
    #insert multiple documents into a collection
    docs = [ {_id: 1, name: 'Steve', hobbies: ['hiking', 'tennis', 'fly fishing'] }, {_id: 2, name: 'Sally', hobbies: ['skiing', 'stamp collecting'] } ]
    
    result = collection.insert_many(docs)
    p result.inserted_count #returns 2

    3. 查询文档

    a. 查询所有文档

    require 'mongo'
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    collection.find. each do |document|
        puts document
    end

    b. 筛选查询

    require 'mongo'
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    puts collection.find({name: 'Sally'} ).first

    4. 更新文档

    a. 更新单个文档

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    result = collection.update_one({name:'Steve'}, 
        {'$set':{phone_number: "444-444-4444"} })
    puts collection.find({name:'Steve'}).first

    b. 更新多个文档

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    result = collection.update_many({}, {'$set'=>{age:36}})
    
    puts result.modified_count

    5. 删除文档

    a. 删除单个文档

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    result = collection.delete_one(name:'Steve')
    puts result.deleted_count

    b. 删除多个文档

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    collection.insert_many([{_id:3,name:"Arnold"},{_id:4,name:"Susan"}])
    puts collection.count
    
    result = collection.delete_many({name:/$S*/})
    puts result.deleted_count

    6. 创建索引

    a. 创建单个索引

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    collection.indexes.create_one({name:1}, unique:true)

    b. 创建多个索引

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    collection = client[:people]
    
    collection.indexes.create_many([
        {key:{name:1}, unique:true},
        {key: {hobbies:1} }
      ])

    7. 运行数据库命令

    require 'mongo'
    #connect to database 'admin'
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/admin')
    db = client.database
    
    #command 'listDatabases'
    p result = db.command({listDatabases:1})

    8. 删除(drop)集合/数据库

    a. 删除集合

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    
    collection = client[:people]
    collection.drop

    b. 删除数据库

    require 'mongo'
    
    client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
    
    client.database.drop

    参考: MongoDB Ruby Driver Manual - Quick Start

  • 相关阅读:
    系统设计的一些原则
    分层开发思想与小笼包
    工作与生活
    Microsoft .NET Pet Shop 4 架构与技术分析
    用人之道(二) 如何管理软件开发团队
    也谈很多开发人员的毛病
    《3S新闻周刊》第10期,本期策划:“超女”营销带来的启示
    浅析ArcIMS
    MapX的坐标问题
    应用ArcIMS构建GMap风格的地图应用
  • 原文地址:https://www.cnblogs.com/RDaneelOlivaw/p/7860107.html
Copyright © 2011-2022 走看看