"Message contains no documents" when trying to insert into Mongodb

I am creating a Ruby on Rails application that uses mongod stone to store data through Mongodb.

My problem comes from this task:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

task :scrape => :environment do

  page = Nokogiri::HTML(open('https://maps.google.com/maps/ms?ie=UTF8&hl=en&source=embed&dg=feature&authuser=0&msa=0&output=kml&msid=208523333872813891131.0004c02beb4f2788337d0'))

  page.css("coordinates").each do |coords|    
    coords = coords.text.split("\n")
    coords.each do |coord|
      array = coord.strip.split(',')
      array[0] = array[0].to_f
      array[1] = array[1].to_f
      if (array[0] != 0 && array[1] !=0)
        puts array[0]
        puts array[1]
        s = Submission.new(array[0], array[1], "Calfire", nil)
        s.insert
      end
    end
  end
end

      

s.insert

tries to remove information from url, instantiate Submission

and then insert it into Mongodb database. Every time I run rake scrape

in terminal, I get the following error when trying to save Submission

to database.

-122.952888
40.741924
rake aborted!
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
  @length=83
  @request_id=3
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="fireapp_development.$cmd"
  @skip=0
  @limit=-1
  @selector={:getlasterror=>1, :w=>1}
  @fields=nil>
failed with error 13066: "Message contains no documents"

      

I'm new to both Ruby and Mongodb, so if any other code is helpful he should be able to update this post. I tried setting up a new app / scaffolding to see if I could fix this, but nothing changed. I also tried to just save the instance Submission

via terminal, but that didn't work either.

When I was looking for a solution, I came across this discussion: https://groups.google.com/forum/#!topic/mongodb-user/kg-wK56_JkQ

This suggests that I may not be asking for anything, but the lines puts

in the Ruby code are clearly being output to the terminal. Any help is appreciated.

EDIT 1:

Model fields Submission

class Submission
    include Mongoid::Document
    field :lat, type: Float
    field :long, type: Float
    field :image, type: Moped::BSON::Binary
    field :category, type: String
end

      

EDIT 2:

The problems were corrected by correct formatting of the string s.create

, as well as removing the string s.insert

.

+3


source to share


1 answer


Standard usage Model.create(attributes)

, you should only do:

s = Submission.create(:lat => array[0], :long => array[1], :category => 'Calfire')
# No Submission.new, no s.insert, just create and check s.valid? or use create!

      



I suspect I Submission.new

really don't know what to do with all of these positional arguments, so you're probably some kind of confused nonsense in s

. The supposed one Submission.new

tries to interpret the arguments and fails silently.

+2


source







All Articles