MongoDB+laravel 查询mongodb集合中数组个数

 {$match: {'process.0': {$exists: true}}},
 {$unwind: '$process'},
 {$match: {'process.correct': 1}},

 {$group: {_id: null, count: {$sum: 1}}}




$db = DB::getMongoDB();
	$collection = $db->exams;

	/*var_dump($collection);
	$document = $collection->findOne();
	var_dump( $document );*/

	$pipeline = array(
		array(
			'$match' => array('process.0' => array('$exists' => true)),
			),
		array(
			'$unwind' => '$process'
			),
		array(
			'$match' => array('process.correct' => 1),
			),
		array(
			'$group' => array(
				"_id" => null,
				"count" => array('$sum' => 1),
				),
			),

	);
	/*{$match: {'process.0': {$exists: true}}},
	{$unwind: '$process'},
	{$match: {'process.correct': 1}},
	{$group: {_id: null, count: {$sum: 1}}}*/
	$result = $collection->aggregate($pipeline);
	var_dump(array_values($result));

参考 http://php.net/manual/en/mongocollection.aggregate.php




你可能感兴趣的:(php+mysql)