我一直在爲自己努力掙扎。恐怕你必須自己建立你的「模型」。讓我們從我的代碼示例:
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
我省略了很多從原來的項目代碼,只是提出了我一直在做的方式。
最重要的這裏曾是行:
places_hash.map { |attrs| Offer.new(attrs) { |o| o.new_record = false } }
都在哪裏我創建的Offers
陣列,但另外,手動我自己new_record
屬性設置爲false
,所以他們的行爲像任何其他文件通過簡單Offer.where(...)
得到。
這不是很美,但它對我很有用,而且我可以充分利用整個Aggregation Framework!
希望有幫助!
你可以顯示查詢思考其他...? – 2015-04-01 11:04:25