Filtering embedded documents in MongoDB

There’s currently no way to filter on embedded docs in the way you’re describing. Using the dot notation allows you to match on an embedded doc, but the entire document, parent and all, will still be returned. It’s also possible to select which fields will be returned, but that doesn’t really help your case, either.

We have a “virtual collections” case, which would implement the desired functionality; feel free to vote on it:

http://jira.mongodb.org/browse/SERVER-142

In the meantime, you should probably treat comments as their own collection. In general, if you need to work with a given data set on its own, make it a collection. If it’s better conceived of as part of some other set, it’s better to embed.

Leave a Comment