Re: [mongodb-user] Projection with a wildcard, for unknown fields...

469 views
Skip to first unread message

Sam Millman

unread,
Dec 12, 2012, 5:23:02 PM12/12/12
to mongod...@googlegroups.com
Linked on SO: http://stackoverflow.com/questions/13848959/mongodb-query-limit-fields-where-name-matches-pattern/13849423#13849423


On 12 December 2012 22:16, NSC <shanno...@cloudsidekick.com> wrote:
I need to retrieve a set of documents, with ALL underscore prefixed fields omitted and all other fields included. Of course this should be done in the query rather than document manipulation after retrieval.

All the operators like $regex, $in, $all seem to apply to values. I need to build a projection that ignores an unknown set of fields based on a pattern in the name. Something like:

db.coll.find({}, {"_*": 0})
 

Of course that doesn't work, but explains the idea.

I can't use positive projection, as I also am not aware of what fields exist in the document.  The schema is dynamic as my end users can edit these documents with user-defined data.

Hope it's easy...

--
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
See also the IRC channel -- freenode.net#mongodb

Derick Rethans

unread,
Dec 12, 2012, 8:47:17 PM12/12/12
to mongod...@googlegroups.com
On Wed, 12 Dec 2012, NSC wrote:

> I need to retrieve a set of documents, *with ALL underscore prefixed fields
> omitted* and all other fields included. Of course this should be done in
> the query rather than document manipulation after retrieval.
>
> All the operators like $regex, $in, $all seem to apply to *values*. I need
> to build a projection that ignores an unknown set of fields based on a
> pattern in the name. Something like:
>
> db.coll.find({}, {"_*": 0})
>
>
> Of course that doesn't work, but explains the idea.
>
> I can't use positive projection, as I also am not aware of what fields
> exist in the document. The schema is dynamic as my end users can edit
> these documents with user-defined data.

If there are always a certain group of fields you want to ignore, then I
suggest creating a sub-document for that group. So in prefixing those
fields with a _, you would make a field "private" which is then an array
containing those fields, f.e.:

from:

{
_id: 'bar';
_private1 : "foo";
}

to:

{
_id: 'bar';
private : {
'field1' : "foo";
}
}

cheers
Derick

--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}
Reply all
Reply to author
Forward
0 new messages