Creating a structure for aggregation

So here is the question. I want to create a data structure given a set of input values.

Since this is a representation of several languages, we consider the list of input data as an array of key / value pairs. And so an array of Hash, Map, Dictionary or any other term that floats on your boat. I will keep all the notation here as JSON, hoping that it is versatile enough for translation / decoding.

So, for input, let's say we have the following:

[ { "4": 10 }, { "7": 9 }, { "90": 7 }, { "1": 8 } ] 

Perhaps a little redundant, but let's stick with that.

So, from this input I want to get into this structure. I give the whole structure, but the important part is what is returned for the value under "weight" :

[
   { "$project": {
       "user_id": 1,
       "content": 1,
       "date": 1,
       "weight": { "$cond": [
           { "$eq": ["$user_id": 4] },
           10,
           { "$cond": [ 
               { "$eq": ["$user_id": 7] },
               9,
               { "$cond": [
                   { "$eq": ["$user_id": 90] },
                   7,
                   { "$cond": [
                       { "$eq": ["$user_id": 1] },
                       8, 
                       0
                   ]}
               ]}
           ]}
       ]}
   }}
]

, , , "", , , .

, , , , , , JSON .

, , user_id.

?

, , , .

, .

.

+2
2

, perl :

use Modern::Perl;

use Moose::Autobox;
use JSON;

my $encoder = JSON->new->pretty;

my $input = [ { 4 => 10 }, { 7 => 9 }, { 90 => 7 }, { 1 => 8 } ];

my $stack = [];

foreach my $item ( reverse @{$input} ) {

  while ( my ( $key, $value ) = each %{$item} ) {
    my $rec = {
      '$cond' => [
        { '$eq' => [ '$user_id', int($key) ] },
        $value
      ]
    };

    if ( $stack->length == 0 ) {
      $rec->{'$cond'}->push( 0 );
    } else {
      my $last = $stack->pop;
      $rec->{'$cond'}->push( $last );
    }

    $stack->push( $rec );
  }

}

say $encoder->encode( $stack->[0] );

, .

  • "", "$ cond" . , "$ user_id" "weight".

  • , ( ), 0, "$ cond" .

  • - (a > 0), "$ cond" .

, , , , . , "" , . , , .

JSON . , , - stack, .

ruby, , OP, :

require 'json'

input = [ { 4 => 10 }, { 7 => 9 }, { 90 => 7 }, { 1 => 8 } ]

stack = []

input.reverse_each {|item|

  item.each {|key,value|
    rec = {
      '$cond' => [
        { '$eq' => [ '$user_id', key ] },
        value
      ]
    }

    if ( stack.length == 0 )
      rec['$cond'].push( 0 )
    else
      last = stack.pop
      rec['$cond'].push( last )
    end

    stack.push( rec )
  }

}

puts JSON.pretty_generate(stack[0])

, , , , OP:

require 'json'

userWeights = [ { 4 => 10 }, { 7 => 9 }, { 90 => 7}, { 1 => 8 } ]

stack = []

userWeights.reverse_each {|item|

  item.each {|key,value|
    rec = {
      '$cond' => [
        { '$eq' => [ '$user_id', key ] },
        value
      ]
    }

    if ( stack.length == 0 )
      rec['$cond'].push( 0 )
    else
      last = stack.pop
      rec['$cond'].push( last )
    end

    stack.push( rec )
  }

}

pipeline = [
    { '$project' => {
        'user_id' => 1,
        'content' => 1,
        'date' => 1,
        'weight' => stack[0]
    }},
    { '$sort' => { 'weight' => -1, 'date' => -1 } }
]

puts JSON.pretty_generate( pipeline )

, , , "", user_id, .

+2

, , . , mongoid, , , Recommended_user_ids - :

    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

, , :

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

Source: https://habr.com/ru/post/1530189/


All Articles