简体   繁体   English

mongodb在php中合并文档

[英]mongodb merging documents in php

I want to merge documents in mongodb with map reduce. 我想将mongodb中的文档与map reduce合并。 There is key "name" is in array if same name key found in other documents it will merge in sorted order. 如果在其他文档中找到相同的名称键,则会在数组中存在键“名称”,它将按排序顺序合并。

Example - Input Mongo dB documents have 4 rows 示例-输入的Mongo dB文档有4行

$data[0]['name'] = "mango";
$data[0]['price'][0]['premium'] = 10;
$data[0]['price'][1]['standard'] = 20;

$data[1]['name'] = "apple";
$data[1]['price'][0]['deluxe'] = 500;
$data[1]['price'][1]['good'] = 700;

$data[2]['name'] = "mango";
$data[2]['price'][0]['good'] = 300;

$data[3]['name'] = "apple";
$data[3]['price'][0]['premium'] = 100;
$data[3]['price'][1]['standard'] = 200;

Output after merge it will be two row 合并后的输出将是两行

$data[0]['name'] = "mango";
$data[0]['price'][0]['premium'] = 10;
$data[0]['price'][1]['standard'] = 20;
$data[0]['price'][2]['good'] = 300;

$data[1]['name'] = "apple";
$data[1]['price'][0]['premium'] = 100;
$data[1]['price'][1]['standard'] = 200;
$data[1]['price'][2]['deluxe'] = 500;
$data[1]['price'][3]['good'] = 700;

Is this possible with mongodb? mongodb有可能吗?

像这样使用Aggregation Framework非常简单:

db.test.aggregate([{$unwind: '$price'}, {$group: {_id: '$name', price: {$push: '$price'}}}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM