如何使用php从mongodb中选择特定的记录


how to select specific records from mongodb using php

我正在使用以下代码:

foreach ($record as $doc)
{
    $groupIds[] = $doc['groupId'];
}
$gpids = "'".implode("','",array_unique($groupIds))."'";
$collection5 = $db->chat;
$cursor = $collection5->find(array('groupId' => array('$in' => array($gpids))));
foreach($cursor as $res)
{
     print_r($res);
}

但不会有结果。请帮帮我。

这是因为您的$gpids是一个字符串,您最终会在$in查询中放入一个元素数组。这应该有效:

$collection5->find(array('groupId' => array('$in' => array_unique($groupIds))));