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

前端 未结 7 447
刺人心
刺人心 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 06:51

    Here how you do it (This would work for 2+ and 3+ as well)

    1) All your Model exhibit this behavior you have include Mongoid::Document inside all your model so technically each document is mapped in monogodb thru moped or mongodb-ruby driver via mongoid

    so If you have model Like

    class PerformerSource 
      include Mongoid::Document
      ## Definition
    
    end
    

    Now you can run Mongo Query using the driver (Moped or Mongodb-ruby driver) like this

    PerformerSource.collection.insert("something")
    ## where something is json document you want to insert
    

    This would give u the moped (if using mongoid 3) connection for that document

    2) You can also do it something like this

     Mongoid::Sessions.default.collections.find { |document| document.name == "performer_sources"}.insert("something")
    

    How to more on mongo query and how mongoid map those using moped u can follow this section of querying where it describe how query is acheived internally via moped

    Hope this help

    0 讨论(0)
  • 2020-12-24 06:53

    If you're using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here's an example of accessing some raw data without using Models to access the data:

    db = Mongoid::Sessions.default
    
    # inserting a new document
    collection = db[:collection_name]
    collection.insert(name: 'my new document')
    
    # finding a document
    doc = collection.find(name: 'my new document').first
    
    # iterating over all documents in a collection
    collection.find.each do |document|
      puts document.inspect
    end
    
    0 讨论(0)
  • 2020-12-24 06:57

    The short answer is Moped. This is the lower-level API that Mongoid is built upon and will be available if you already use Mongoid. The Moped API is a thin wrapper around the raw MongoDB operations. The documentation here: http://mongoid.org/en/moped/docs/driver.html should be useful.

    0 讨论(0)
  • 2020-12-24 07:01

    For Mongoid 6:

    db = Mongoid::default_client
    collection = db[:collection_name]
    
    0 讨论(0)
  • 2020-12-24 07:10

    For Mongoid 5:

    db = Mongoid::Clients.default
    
    collection = db[:collection_name]
    

    Now we can perform queries on the collection

    0 讨论(0)
  • 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}"
    
    0 讨论(0)
提交回复
热议问题