0

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 :(

3 Answers 3

2

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], ...])
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

Hi, i understand but it didnt Work with my Class, any Ideas? Thank you. ;-) public function mongoGet($f) { $cursor = $this->MoTbl->find($f); $k = array(); $i = 0; while($cursor->hasNext()) { $k[$i] = $cursor->getNext(); $i++; } return $k; }
@lun4tic What do you mean it didn't work? What Class? Can you show some code?
Sorry, im a little confused today. ;-) I call the function: $m->mongoGet(array(), $fields); In this example, i receive all field, not the specified in $fields Array.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.