28

I am having trouble grasping how to filter embedded documents in MongoDB, and am starting to think I should be using a relational association, but that feels wrong in the document-store context.

Sticking with a typical blog/comment system, I have a collection of blogs, and each blog has many comments. The comments are stored as embedded documents inside the blog document.

It is very simple to filter my blogs collection, but in order to filter my comments embedded in each blog, I am having to load them all into memory (retrieve all into a Ruby array), and loop through each comment, returning ones that match a specific criteria.

My efforts to filter embedded documents using dot notation is failing, and bringing back all sub documents.

Is there a better way of getting MongoDB to filter these for me, or should I resign myself to relational associations? (Pulling back all embedded documents and manually filtering is going to be too intensive in the long run)

Community
  • 1
  • 1
kez
  • 575
  • 1
  • 6
  • 14

1 Answers1

22

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.

Kyle Banker
  • 4,359
  • 23
  • 18
  • Thanks kb - have gone with the collection on its own, seems to be working okay so far; just need to stress test it a bit. – kez Jan 26 '10 at 16:08
  • Cool. It should still be efficient. – Kyle Banker Jan 26 '10 at 18:35
  • 13
    Isn't "virtual collections" a great overkill to this very needed requirement? I'm just curious - is the ability to return only a specific embedded document not being developed because it is not needed or because it is complicated? – idophir Nov 16 '11 at 21:57
  • 5
    very good question by idophir. I am also wondering why Mongo has not implemented that. – user644745 Jan 01 '12 at 06:43
  • 1
    Can it be achieved in Cassandra? – Manish Jun 12 '16 at 11:08