Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:45:30+00:00 2026-05-22T01:45:30+00:00

I’m trying to use ruby and mongoid in order to extract some data from

  • 0

I’m trying to use ruby and mongoid in order to extract some data from an oracle database and into my mongoDB in order to perfom a couple of operations on it.

The question is:

I created my ‘Record’ class with includes the Mongoid::Document and set up all my fields, and have already assigned the data coming out of the oracle database, and have all my BSON objects stored in an array.

Now my question is: How I save them?

Here’s my piece of code

query = db.report # Sequel Object

query.each do |row|
  r = Record.new #Mongoid class
  r.directory_name    = row[:directory_name]
  r.directory_code    = row[:directory_id]
  r.directory_edition = row[:edition]
  r.last_updated      = row[:updated]
  r.canvass           = row[:canvass_id]
  r.specialty_item    = row[:item]
  r.production_number = row[:prodnr]
  r.status            = row[:exposure_status]
  r.scanned_date      = row[:scandate]
  r.customer_id       = row[:customer_id]
  r.sales_rep         = row[:sales_rep_name]
  r.phone             = row[:phone]
  r.customer_name     = row[:customer_name]
  records << r
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:45:31+00:00Added an answer on May 22, 2026 at 1:45 am

    You would need to do Record.collection.insert(records). Although note that this will skip any validations you have written in your Mongoid model but will be a little faster than creating mongoid records and saving them, as it will use the ruby mongo driver directly. You should only do this if you know that data is consistent.

    If you want to do all the validations on your data before saving them in MongoDB, you should create a model instead of putting them in an array.

    So you can persist the data extracted in MongoDB in three ways according to your preferences:

    • Insert all records at once using mongo driver, but beware the array you are creating can be huge:
    query.each do |row|
      .....
    end
    Record.collection.insert(records)
    
    • Insert one record at a time using mongo driver(replace records << r with new line)
    query.each do |row|
      .....
      Record.collection.insert(r)
    end
    
    • Insert one record at a time using Mongoid and all the validations and callbacks(replace records << r with new line)
    query.each do |row|
      .....
      r.save
    end
    

    update: Missed that you are already creating the record hence the mongo driver suggestions. If you want to use mongo driver directly, you should use a hash instead of Mongoid model. i.e instead of

    r = Record.new
    r.status = row[:status]
    # copy more data
    

    you should do

    r = {}
    r[:status] = row[:status]
    # copy more data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.