[mongodb-user] filtering returned portion of find query to only what was matched

121 views
Skip to first unread message

Wes

unread,
Apr 28, 2010, 2:25:03 AM4/28/10
to mongodb-user
My scenario is explained below, but the application and data are just
an example.

{
_id : "1",
blog_name : "My First Blog",
entries : [
{
entry_id : "123",
entry_name : "My first entry",
posted : "2010-01-01",
comments : [ { user : "bob", comment : "cool"}, { user : "joe",
"not cool" } ]
},
{
entry_id : "124",
entry_name : "My second entry",
posted : "2010-01-01",
comments : [ { user : "jane", comment : "not cool"}, { user :
"sam", "not cool" } ]
},
{
entry_id : "125",
entry_name : "My third entry",
posted : "2010-01-02",
comments : [ { user : "jane", comment : "not cool"}, { user :
"sam", "not cool" } ]
}
]
}

db.blogs.find( { entries : { $elemMatch : { posted : "2010-01-01",
comments.comment : "cool" } } } )

This matches on entry 123 but entries 124 and 125 get returned also.
Is there a way to omit 124 and 125?
I really only want what matches. I realize that the entire entry in
the collection is being matched, but I
only want the subset that matches my query to be returned. I am hoping
my approach is wrong. The other
posts I have read are leading me to believe it is not possible.

Any ideas?

Would map reduce be appropriate for something like this? I understood
that mongo's implementation can
only handle returning numbers and simple objects. I would be returning
a blog entry with a nested array
of comments with the above scenario.

Thanks in advance for any help.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Michael Dirolf

unread,
Apr 28, 2010, 8:04:57 AM4/28/10
to mongod...@googlegroups.com
I think you're looking for this functionality:
http://jira.mongodb.org/browse/SERVER-828

Feel free to vote/track there.

Wes

unread,
Apr 28, 2010, 11:12:08 AM4/28/10
to mongodb-user
Yes, the description matches what I was trying to find.

Returning the whole matching path:

{
_id : "1",
blog_name : "My First Blog",
entries : [
{
entry_id : "123",
entry_name : "My first entry",
posted : "2010-01-01",
comments : [ { user : "bob", comment : "cool"}, { user : "joe",
"not cool" } ]
}
]
}

Or just the matching element:

{
entry_id : "123",
entry_name : "My first entry",
posted : "2010-01-01",
comments : [ { user : "bob", comment : "cool"}, { user : "joe",
"not cool" } ]
}

would provide what I need in this scenario. It would be nice to be
able to specify which was desired.

Thanks for pointing me in the right direction.
> > For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
Reply all
Reply to author
Forward
0 new messages