Inserting array as single rows into mongodb using mongoid?

By : Tronic
Source: Stackoverflow.com
Question!

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

thanks

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

By : Tronic


Answers
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 = Mongo::Connection.new
collection = c['test']['insert_test']

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

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


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