在mongodb中按字符串和数组值分组

时间:2016-07-22 06:03:02

标签: php mongodb

我有一张看起来像这样的表:

[
    {
        username: 'doug'
        hashtags: ['one', 'two', 'three']
    },
    {
        username: 'cait',
        hashtags: ['three', 'seven']
    },
    {
        username: 'lyn',
        hashtags: ['eight', 'one', 'nine']
    },
    {
        username: 'jake',
        hashtags: ['a', 'b', 'c']
    },
    {
        username: 'paul',
        hashtags: ['four', 'eight', 'three']
    },
    {
        username: 'cait',
        hashtags: ['six']
    },
    {
        username: 'cait',
        hashtags: ['six', 'nine']
    },
    {
        username: 'lyn',
        hashtags: ['ball']
    },
    {
        username: 'doug',
        hashtags: ['cart', 'mart']
    }
]

我希望只能选择具有特定主题标签的特定人员:

[
    {
        username: 'cait',
        hashtag: 'three'
    },
    {
        username: 'doug',
        hashtag: 'two'
    },
    {
        username: 'cait',
        hashtag: 'six'
    }
]

我正在尝试在PHP上实现它,我目前有以下代码:

$match = [];
$match['$or'] = [
  {
    username: 'cait',
    hashtags: 'three'
  },
  {
    username: 'doug',
    hashtags: 'two'
  },
  {
    username: 'cait',
    hashtags: 'six'
  }
];

$group = [
  '_id' => [
    'username' => '$username',
    'hashtags' => '$hashtag'
  ],
  'counts' => ['$addToSet' => '$_id']
];

$project = [
  '_id' => 1,
  'counts' => ['$size' => 'counts']
];


$aggregate = [
  ['$match' => $match]
];

$aggregate[] = ['$group' => $group];

$aggregate[] = ['$sort' => []]; 

$aggregate[] = ['$project' => $project];
$data = $this->mongodb
  ->collection($collection)
  ->raw()
  ->aggregate($aggregate);    

return $data['result'];

问题是它返回4行而不是我期望的3行:

[
    {
        _id: {
            username: 'cait',
            hashtags: ['three', 'seven'],
        },
        counts: 1
    },
    {
        _id: {
            username: 'cait',
            hashtags: ['six']
        },
        counts: 1
    },
    {
        _id: {
            username: 'cait',
            hashtags: ['six', 'nine']
        },
        counts: 1
    },
    {
        _id: {
            username: 'doug'
            hashtags: ['one', 'two', 'three']
        }, 
        counts: 1
    }
]

我理想的结果应如下所示:

[
    {
        _id: {
            username: 'cait',
            hashtags: ['three', 'seven']
        },
        counts: 1
    },
    {
        _id: {
            username: 'cait',
            hashtags: ['six']
        },
        counts: 2
    },
    {
        _id: {
            username: 'doug'
            hashtags: ['one', 'two', 'three']
        },
        counts: 1
    }
]

提前致谢。

0 个答案:

没有答案