我如何在mongodb find()游标中返回特定字段?

I try:

$fields = array('name'=>true);
find (array(array(), $fields))

but it's not working (I get nothing) and I can't see my mistake. Sorry :(

The PHP function for find does not work like that. Try:

find(array(), array('name'=>1))

(basically omit the surrounding array)

For reference here is the documentation page: http://php.net/manual/en/mongocollection.find.php

If you are looking for a specific field in all your records you can do something like this:

$cursor = $collection->find();
    foreach( $cursor->fields(array('myField' => true )) as $doc ) {
        echo "<pre>";
        var_dump($doc);
        echo "</pre>";

This would return only 'myFeild' for each document in the collection.

http://php.net/manual/en/mongocursor.fields.php

If you use mongodb/mongodb php composer package, you need to specify projection option:

find([], ['projection' => ['name' => false]])

Another example of options:

find([], ['limit' => 5, projection' => ['name' => false], ...])