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

  • 相关阅读:
    Benchmarking Apache Kafka, Apache Pulsar, and RabbitMQ: Which is the Fastest?
    Kafka实战:集群SSL加密认证和配置(最新版kafka-2.7.0)
    Postgresql 编译安装教程
    CentOS在线和离线安装PostgreSQL
    ubuntu apt-get update连不上dl.google.com解决方法
    ubuntu E: Sub-process /usr/bin/dpkg returned an error code (1)解决办法
    ubuntu apt-get更新出现W: GPG error: http://repo.mysql.com trusty InRelease
    hadoop3.2.2 ERROR: but there is no HDFS_NAMENODE_USER defined. Aborting operation. Starting datanodes
    Hudi on flink v0.7.0 使用遇到的问题及解决办法
    RocksDB in Flink官方答疑:Using RocksDB State Backend in Apache Flink: When and How
  • 原文地址:https://www.cnblogs.com/RDaneelOlivaw/p/7860107.html
Copyright © 2011-2022 走看看