2

Say you have a blog collection like this

{"title": "Post Title", "body": "Post Body", "comments": [{"user": "user1", "comment": "comment from user1"}, {"user": "user2", "comment": "comment from user2"}]} {"title": "Post Title 2", "body": "Post Body 2", "comments": [{"user": "user1", "comment": "comment from user1"}]}

How can I query for the comments from a user?

This query works but returns comments from user2 as well as user1. db.posts.find({"comments.user": "user1"})

Is there any way to get only the comments from user1?

respectTheCode
  • 42,348
  • 18
  • 73
  • 86
  • I asked the same question a few months ago: http://stackoverflow.com/questions/5496714 – Rich Sep 02 '11 at 14:23

2 Answers2

2

I don't think there is a way since Mongo just returns the whole document when it finds a match, you may be able to do it with Map/Reduce or server processing

jeffsaracco
  • 1,259
  • 12
  • 21
1

This feature request can probably help you... when it will be implemented (some months if no delay).

https://jira.mongodb.org/browse/SERVER-828

Aurélien B
  • 4,590
  • 3
  • 34
  • 48
  • So I guess they missed the less than a week about 7 weeks ago. Hopefully this will be released someday, but until then I guess I am stuck with the Map/Reduce that jeff suggested. – respectTheCode Sep 02 '11 at 22:37