Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering embedded documents in MongoDB

Tags:

mongodb

nosql

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)

like image 297
kez Avatar asked Jan 26 '10 09:01

kez


People also ask

How do I query embedded files in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

How do I filter records in MongoDB?

Parameter description syntax of filter operator in MongoDB. In the input parameter, we have passed the array field to filter the documents. We need to use the $ sign before using the input field parameter in the filter operator. 3) As – It is an optional parameter used in the filter operator.

How do I query a nested object in MongoDB?

MongoDB Nested Query Match on a Nested Field You can use the dot notation (“field. nestedField”) to specify query criteria for fields in embedded/nested documents. For queries that use dot notation, fields and nested fields must be enclosed in double-quotes.

What is embedded document in MongoDB?

MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document.


1 Answers

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.

like image 57
Kyle Banker Avatar answered Sep 28 '22 10:09

Kyle Banker