• 使用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

  • 相关阅读:
    考研打卡_Day049
    考研打卡_Day048
    【生活】2017 开始!
    朝花夕拾-android 自定义toast
    朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态
    android media server 解析1-media player service 结构部分
    android binder 进程间通信机制6-Binder进程间通信机制的JAVA接口
    android binder 进程间通信机制5-Service注册和代理对象的获取
    android binder 进程间通信机制4-Service Manager
    android binder 进程间通信机制3-Binder 对象生死
  • 原文地址:https://www.cnblogs.com/RDaneelOlivaw/p/7860107.html
Copyright © 2020-2023  润新知