How to query MongoDB directly from Ruby instead of using Mongoid?

前端 未结 7 478
刺人心
刺人心 2020-12-24 06:33

I am writing a migration for a Rails application that uses MongoDB and Mongoid. My migration currently uses my models that use Mongoid to query and update records, but the p

7条回答
  •  无人及你
    2020-12-24 07:16

    Like anyone has mention here , your answer is Moped. Here is my example for a ruby script (simple file test.rb)

    1. Define a mongoid.yml (in this case , at localhost)

    development: sessions: default: database: test_development hosts: - localhost:27017 options: 2. Set load config and test collection

    #!/usr/bin/env ruby
    require 'mongoid'
    
    Mongoid.load!("path/to/file/mongoid.yml",:development) # :development corresponds to mongoid.yml first line environment
    db = Mongoid::Sessions.default
    puts "Collection documents count :> #{db[:collection].find.count}"
    

提交回复
热议问题