mongodb - The output of mongo is non-deterministic with native-ruby-mongo-mapper -
i ran problem happens randomly.
first, exsit?(collection, id)
returned true
it means there has document in mongodb, have further check in next step
then (coll.find({_id: id}).first['history'].last['price']
raise error
#<nomethoderror: undefined method `last' nil:nilclass>
i don't why happens randomly.
code
def exsit?(collection, id) return (collection.find({_id: id}).first.nil? ) ? false : true end def is_price_changed?(coll, id, current_price) if exsit?(coll, id) return (coll.find({_id: id}).first['history'].last['price'] != current_price)? true : false else return true end end
this document format
{ "updated_at": new date(1435757280839), "price": 16890, "history": [ { "updated_at": new date(1435757277672), "price": 16890 } ] }
it looks 'history' attribute of id nil
Comments
Post a Comment