我无法使用MongoDB进行正确排序。我基本上想要在分组后对分组结果进行排序。
例如:我想通过手机对结果进行分组,但每部手机可能有100笔交易。我希望在结果分组之前按手机成本和线路租金对这些进行排序。
这是我到目前为止所做的:
array(
'$group' => array(
'_id' => '$handset_numeric_id',
'full_handset_name' => array('$push' => '$full_handset_name'),
'deal_id' => array('$push' => '$deal_id'),
'handset_url' => array('$push' => '$handset_url'),
'deal_url' => array('$push' => '$deal_url'),
'image_url' => array('$push' => '$image_url'),
'phone_cost' => array('$push' => '$phone_cost'),
'tariff_name' => array('$push' => '$tariff_name'),
'minutes' => array('$push' => '$minutes'),
'texts' => array('$push' => '$texts'),
'data' => array('$push' => '$data'),
'effective_monthly_cost' => array('$push' => '$effective_monthly_cost'),
'network_numeric_id' => array('$push' => '$network_numeric_id'),
'network_name' => array('$push' => '$network_name'),
'manufacturer_name' => array('$push' => '$manufacturer_name'),
),
),
array(
'$sort' => array(
'phone_cost' => 1,
'effective_monthly_cost' => 1
)
),
);
对我来说,解决这个问题的最佳途径是什么?
更新:
这是我得到的结果:
Samsung Galaxy S III Mini amber Orange 0 47 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 38.67 The Works 47 (24m) 5000
Samsung Galaxy S III Mini black Orange 0 42.83 The Works 47 (24m) 5000
Samsung Galaxy S III amber Orange 0 47 The Works 47 (24m) 500
正如您在上面所看到的,MongoDB已经选择了三星Galaxy Y S5360白色,价格为38.67英镑,但这不是最便宜的每月有效费用。
如果查看下面的数据,您会看到最便宜的有效月费为27.21英镑
Samsung Galaxy Y S5360 white Orange 0 27.21 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 28.46 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 29.29 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 31.38 The Works 52 (24m) 8000
Samsung Galaxy Y S5360 white Orange 0 31.38 The Works 52 (24m) 8000
Samsung Galaxy Y S5360 white Orange 0 32.21 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 33.67 The Works 52 (24m) 8000
Samsung Galaxy Y S5360 white Orange 0 34.5 The Works 52 (24m) 8000
Samsung Galaxy Y S5360 white Orange 0 38.67 The Works 47 (24m) 5000
Samsung Galaxy Y S5360 white Orange 0 47 The Works 47 (24m) 5000
我输入的数据示例:
array (
'deal_id' => '815300000000',
'handset_numeric_id' => 305110,
'handset_url' => 'samsung-galaxy-s4-active-grey',
'deal_url' => 'samsung-galaxy-s4-active-grey?type=paym&deal=m056030510815300000000',
'image_url' => 'samsung-galaxy-s4-active-grey',
'handset_id' => 'galaxys4activegrey',
'phone_cost' => 0,
'tariff_numeric_id' => 814553,
'tariff_id' => 'talktalk300-3000-1500-354441.24',
'tariff_name' => '300 mins and 3000 texts with 1.5GB (24m)',
'network_name' => 'TalkTalk',
'network_numeric_id' => 1345,
'term' => 24,
'minutes' => 300,
'texts' => 3000,
'data' => 1500,
'org_line_rental' => 35,
'effective_monthly_cost' => 35,
'total_cost_over_term' => 840,
'gift_category' => 'Free Connection',
'gift_category_numeric_id' => 2,
'free_gift_id' => 0001,
'free_gift_name' => 'FREE Sony PS3 Super Slim 500GB',
'cashback' => 0,
'free_lr' => 0,
'half_lr' => 0,
'included_cashback' => 0,
'free_lr_number' => 0,
'half_lr_number' => 0,
'free_half_amount' => 0,
'clearance_flag' => 0,
'manufacturer_numeric_id' => 2,
'manufacturer_name' => 'Samsung',
'full_handset_name' => 'Samsung Galaxy S4 Active grey',
'popularity' => 15534234202,
'handset_colour' => 'grey',
'tariff_type' => 'paym',
'feature_ids' => '4,w,b,x,c,8,v,t,r,l,q,i,',
'operating_system' => 'Android OS v4.2.2',
'retailer_id' => 'talktalk',
'retailer_name' => 'TalkTalk',
)
答案 0 :(得分:0)
问题在于您在分组完成后有效排序。如果电话或线路费用因1个手机(例如手机A)而异,那么您应该在分组键(_id)中包含费用。目前,您只需为手机A创建一系列电话费和线路租赁费用。
对于您想要做的事情,最好的方法是将价格和线路租金包含在您的分组键中。用以下内容替换你的组_id键。
$group => array( '_id' => array(handsetId => '$author', phone_cost => '$phone_cost', effective_monthly_cost=> '$effective_monthly_cost' ),
...
然后在聚合管道的下一步中,您可以按电话费用或线路租赁进行排序。
希望有所帮助