Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to exclude a field #147

Closed
kschroeder opened this issue Mar 29, 2016 · 3 comments
Closed

Unable to exclude a field #147

kschroeder opened this issue Mar 29, 2016 · 3 comments
Labels

Comments

@kschroeder
Copy link

I might just be missing this in the code but I'm having some difficulty figuring out how to exclude subdocuments from a result. One of the collections I'm working with can have fairly extensive array of subdocuments and the unserialize() functionality takes a fair amount of resources. I've moved the functionality into an aggregate() function (which is awesome BTW) but I would like to omit retrieving those subdocuments from my normal queries so I don't have to bear with the overhead of unserialization.

In the old MongoDB Collection I could specify

db.inventory.find(
   { type: 'food', _id: 3 },
   { "classification.category": 0}
)

but I can't find how that can be accomplished with this library.

@jmikola
Copy link
Member

jmikola commented Mar 29, 2016

The Collection::find() and Collection::findOne() methods take a projection option, which corresponds to the second argument in your example. This is equivalent to the projection option on the MongoDB\Driver\Query object in the underlying driver.

This will be properly documented in #144. To copy/paste an example from that PR:

$document = $collection->findOne(
    ['_id' => '10011'],
    ['projection' => ['pop => 1']]
);

@kschroeder
Copy link
Author

And there's 5 seconds off my elapsed execution time. Thanks a bunch.

@jmikola
Copy link
Member

jmikola commented Mar 29, 2016

Note: this option is now documented in the Collection::find() documentation (along with findOne()). Additionally, I added a section about projections to the CRUD tutorial).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants