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

Comments

Projects
None yet
2 participants
@kschroeder
Copy link

kschroeder commented Mar 29, 2016

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

This comment has been minimized.

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']]
);

@jmikola jmikola added the question label Mar 29, 2016

@kschroeder

This comment has been minimized.

Copy link

kschroeder commented Mar 29, 2016

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

@kschroeder kschroeder closed this Mar 29, 2016

@jmikola

This comment has been minimized.

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