mongodb - Ruby datajob with mongoid -
i'm trying use ruby , mongoid in order extract data oracle database , mongodb in order perfom couple of operations on it.
the question is:
i created 'record' class includes mongoid::document , set fields, , have assigned data coming out of oracle database, , have bson objects stored in array.
now question is: how save them?
here's piece of code
query = db.report # sequel object query.each |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
you need record.collection.insert(records)
. although note skip validations have written in mongoid model little faster creating mongoid records , saving them, use ruby mongo driver directly. should if know data consistent.
if want validations on data before saving them in mongodb, should create model instead of putting them in array.
so can persist data extracted in mongodb in 3 ways according preferences:
- insert records @ once using mongo driver, beware array creating can huge:
query.each |row| ..... end record.collection.insert(records)
- insert 1 record @ time using mongo driver(replace
records << r
new line)
query.each |row| ..... record.collection.insert(r) end
- insert 1 record @ time using mongoid , validations , callbacks(replace
records << r
new line)
query.each |row| ..... r.save end
update: missed creating record hence mongo driver suggestions. if want use mongo driver directly, should use 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
Comments
Post a Comment