Re: Problems selecting specific fields with Perl

154 views
Skip to first unread message

aliane abdelouahab

unread,
Sep 26, 2012, 6:47:16 PM9/26/12
to mongodb-user
i dont know if it's the same problem i got with pymonog, but because
you're dealing with nested documents, then you must add the dollar
sign

On 26 sep, 23:28, Smiffy <smiffyt...@gmail.com> wrote:
> Hi Folks
>
> I am using the MongoDB-0.46.2 CPAN module, and am having problems querying
> for specific fields.
>
> I have the following data:
>
> {
>   "_id" : ObjectId("50637b8017f354712f000000"),
>   "phleem" : {
>           "nox" : "nix",
>           "ni" : "noo"
>   },
>   "foo" : {
>           "bar" : NumberLong(123),
>           "baz" : NumberLong(456)
>   }
>
> }
>
> If I perform the following query from the shell, I get the
> desired/anticipated result:
>
> > db.test.findOne({ 'foo.bar' : 123}, { 'foo.baz' : 1, 'phleem.nox' : 1 })
>
> {
>   "_id" : ObjectId("50637b8017f354712f000000"),
>   "phleem" : {
>           "nox" : "nix"
>   },
>   "foo" : {
>           "baz" : NumberLong(456)
>   }
>
> }
>
> However, here's what happens when I try to do what I think is the same query
> from Perl:
>
> print Dumper(
>   $c->find(
>     { 'foo.bar' => 123},
>     { 'foo.baz' => 1, 'phleem.nox' => 1 }
>   )->all
> );
>
> ...which gives me:
>
> $VAR1 = {
>   'phleem' => {
>                 'ni' => 'noo',
>                 'nox' => 'nix'
>               },
>   '_id' => bless( {
>                     'value' => '50637b8017f354712f000000'
>                   }, 'MongoDB::OID' ),
>   'foo' => {
>              'bar' => 123,
>              'baz' => 456
>            }
>
> };
>
> I used the "SELECT a,b FROM users WHERE age=33" example from here:http://search.cpan.org/~friedo/MongoDB-0.46.2/lib/MongoDB/Examples.pod
>
> Why am I getting the full document, rather than just the specified fields?
>
> Cheers
>
> M

aliane abdelouahab

unread,
Sep 26, 2012, 6:48:34 PM9/26/12
to mongodb-user

David K Storrs

unread,
Sep 27, 2012, 3:23:32 PM9/27/12
to mongodb-user
Bernie nailed it and provided the appropriate links, but here's a code
sample for clarity:

my $all_foo_data = $db->coll->find({ _id =>
ObjectId("50637b8017f354712f000000") })->fields({ foo => 1 })->next;

my $foo_nox_data = $db->coll->find({ _id =>
ObjectId("50637b8017f354712f000000") })->fields({ 'foo.nox' => 1 })-
>next;

Code is untested, but should give you a starter.

Dave

Smiffy

unread,
Sep 27, 2012, 5:28:39 PM9/27/12
to mongod...@googlegroups.com
On Friday, September 28, 2012 4:53:45 AM UTC+9:30, David K Storrs wrote:
...

Code is untested, but should give you a starter.

I modified my original with a deliberate suppression, just to prove it both ways, not that I'd want to suppress _id in real life:

print Dumper(
  $c->find( { 'foo.bar' => 123} )->fields( { 'foo.baz' => 1, 'phleem.nox' => 1, '_id' => 0 } )->all
);

Gives:

{
  'phleem' => {
                'nox' => 'nix'
              },
  'foo' => {
             'baz' => 456
           }
}

Yay! Thanks, folks, that's just what I needed :-)



Reply all
Reply to author
Forward
0 new messages