Generating a Structure for Aggregation

前端 未结 2 738
心在旅途
心在旅途 2020-11-29 13:16

So here\'s a question. What I want to do is generate a data structure given a set of input values.

Since this is a multiple language submission, let\'s consider the

2条回答
  •  时光取名叫无心
    2020-11-29 13:27

    First thank you Neil for your help with this here, this workout great for me and it's really fast. For those who use mongoid, this is what I used to create the weight parameter where recommended_user_ids is an array:

        def self.project_recommended_weight recommended_user_ids
          return {} unless recommended_user_ids.present?
          {:weight => create_weight_statement(recommended_user_ids.reverse)}
        end
    
        def self.create_weight_statement recommended_user_ids, index=0
          return 0 if index == recommended_user_ids.count
          {"$cond" => [{ "$eq" => ["$user_id", recommended_user_ids[index]] },index+1,create_weight_statement(recommended_user_ids,index+1)]}
        end
    

    So to add this to the pipeline simply merge the hash like this:

    {"$project" => {:id => 1,:posted_at => 1}.merge(project_recommended_weight(options[:recommended_user_ids]))}
    

提交回复
热议问题