joedevon
joedevon

Reputation: 2689

How do you query an embedded document in MongoDB returning a subset of results?

I've got a data structure like so:

Post
{
'id': $_id,
'user' : ['first_name' : 'Joe', 'last_name' : 'Devon' ],
'text' : 'blah',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'hello'],
  ['first' : 'John', 'last' : 'Smith', 'comment' : 'bye', 'hidden' : true],
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'world']
},
{
'id': $_id,
'user' : ['first_name' : 'Joe', 'last_name' : 'Shmoe' ],
'text' : 'meh',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'sup'],
},
{
'id': $_id,
'user' : ['first_name' : 'Mr.', 'last_name' : 'Smith' ],
'text' : 'bah',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'sup mon'],
}

I'm trying to run a query that will return everything EXCEPT the comment that has 'hidden':true.

Tried everything that doesn't work. Looking for the one command that will work. Help please :)

Upvotes: 0

Views: 259

Answers (1)

Tyler Brock
Tyler Brock

Reputation: 30146

This is currently impossible to do with mongodb and you will have to filter comments on the client side.

The filtering mechanisms only serve to match or not match whole documents and then retrieve a subset of their fields but unfortunately you can't specify criteria for which of those to return.

If you had a collection for comments you could filter out the ones that have hidden: true.

Upvotes: 2

Related Questions