Inserting array as single rows into mongodb using mongoid?

By : Tronic

i have a ruby array of values. how can i insert them as single entries int omy model using mongoid? any advice?


edit: okay, i've done it. just converted the file into a csv and imported it using mongoimport :) thanks anyway!

By : Tronic

Mongoid in a Rails environment (access the ruby-mongo-driver collection):

collection = YourModel.collection

Ruby-mongo-driver (standalone ruby script):

require 'rubygems'
require 'mongo'
# Assumes MongoDB running at default settings on the same machine
c =
collection = c['test']['insert_test']

The actual insert (same code for both the snippets above, choose one):

values = ['foo', 'bar', 'baz']
documents = { |v| { :value => v } }
# [{"_id"=>BSON::ObjectId('4dae84284499d11e00000001'), "value"=>"foo"}, ...]
By : Matt

This video can help you solving your question :)
By: admin