由于驱动程序版本,目前有很多关于mongodb和php的帖子。我在XAMPP上使用驱动程序1.2.7,php 5.6和最新的mongodb
这是我目前的代码:
$filter = [ 'userID' => $myUserId, 'isSold' => true];
$cmdOne = new MongoDB\Driver\Command([
'distinct' => 'collectionNameHere',
'key' => 'productID',
'query' => $filter
]);
$cursorOne = $connection->executeCommand('DatabaseNameHere', $cmdOne);
$products = current($cursorOne->toArray())->values;
有没有办法找到非截然不同的结果?
由于mongoDb驱动程序不同,这不起作用: MongoDB search in collection
答案 0 :(得分:1)
您必须将executeQuery与查询过滤器和投影一起使用
$filter = [ 'userID' => $myUserId, 'isSold' => true];
$projection = ['projection' => ['productID' => 1]];
$query = new MongoDB\Driver\Query($filter, $projection);
$cursor = $connection->executeQuery('DatabaseNameHere.CollectionNameHere', $query);