Find a document according to its parent's attributes
Supposing I have two collections, posts and comments.
post = { _id: 1,
language: 'en',
title: ...,
author: ... }
comment = { _id: 2345,
parent: 1,
title: ...,
author: ... }
Is there another way to find all the documents (comments) whose parent has the language 'en' apart from finding all the comments, and then querying on their parent attribute to find out the language?
If not, is there a way to present the document differently to make such a query easier to perform? (In that case, I made a different collection for the comments, instead of a single document per post, because it is easier and in my mind more efficient to fe开发者_StackOverflow社区tch one comment, than retrieving the whole document and filtering it).
First of all, there is no shortcut to do what you want. So the answer to your first question is no.
With NoSQL databases normalization is not very important. In this case you want to query comments based on language code so I'd simply add the language code to the comment as well. You know for which post your adding a comment app-side so you can simply store the post's language if you want.
As for your schema; having a document per comment may be the best solution for you if you regularly show single comments. In most other situations you always show at least a significant amount of comments in which case it's usually better to have a collection of comments per document. Say a document per comment page. Makes for very snappy pagination as well. If you don't expect many comments per post you can just embed them.
精彩评论