Ruby mongoid aggregation return object -


i doing mongodb aggregation using mongoid, using modlename.collection.aggregate(pipeline) . value returned array , not mongoid::criteria, if first on array, first element of type bson::document instead of modelname. result, unable use model.

is there method return criteria instead of array aggregation, or convert bson document model instance?

using mongoid (4.0.0)

i've been struggling on own too. i'm afraid have build "models" on own. let's take example code:

class searcher   # ...    def results(page: 1, per_page: 50)     pipeline = []      pipeline <<        "$match" => {         title: /#{@params['query']}/i       }     }      geonear = {       "near"               => coordinates,       "distancefield"      => "distance",       "distancemultiplier" => 3959,       "num"                => 500,       "spherical"          => true,     }      pipeline << {       "$geonear" => geonear     }      count = aggregate(pipeline).count      pipeline << { "$skip" => ((page.to_i - 1) * per_page) }     pipeline << { "$limit" => per_page }      places_hash = aggregate(pipeline)      places = places_hash.map { |attrs| offer.new(attrs) { |o| o.new_record = false } }      # ...      places   end    def aggregate(pipeline)     offer.collection.aggregate(pipeline)   end end 

i've omitted lot of code original project, present way i've been doing.

the important thing here line:

places_hash.map { |attrs| offer.new(attrs) { |o| o.new_record = false } } 

where both i'm creating array of offers, additionally, manually i'm setting new_record attribute false, behave other documents simple offer.where(...).

it's not beautiful, worked me, , take best of whole aggregation framework!

hope helps!


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -