Perlator
Perlator

Reputation: 241

MongoDB/Perl: find_one doesn't return data after unrelated code

mongodb is v4.0.5

Perl is 5.26.3

MongoDB Perl driver is 2.0.3

This Data::Dumper output shows what's driving me crazy

INFO - $VAR1 = [
  '275369249826930689 1',
  {
    'conf' => {
      'param' => 'argument'
    },
    'id' => '275369249826930689',
    'lastmsg' => '604195211232139552',
    '_id' => bless( {
      'oid' => ']:\',&�h�GeR'
    }, 'BSON::OID' )
  }
];

352832438449209345 275369249826930689
INFO - $VAR1 = [
  '275369249826930689 2'
];

The second INFO - $VAR1 should show the same content as the first one. This is the original code, which I have (see below) broken down to find the culprit.

    ddump(["$userid 1",
        $c_identities->find_one({
            channel => 'chan1',
            id      => $userid,
        })
    ]);
    my @filtered = reverse      
                   grep { $_->{author}->{id} == $userid } @{$answers};

    ddump(["$userid 2",
        $c_identities->find_one({
            channel => 'chan1',
            id      => $userid,
        })
    ]);

ddump is just a wrapper for Data::Dumper. If I remove the "my @filtered" line, the second find one again returns the expected result (a MongoDB document). $answers is just a listref of hashes - no objects - from some API, completely unrelated to MongoDB.

So I broke the "reverse grep" code down to see where the culprit is. The say are the two numbers you see between the dumpers above. This is what I can do, to get answer from the second find_one:

    for my $answer (@{$answers}) {
        say $answer->{author}->{id}, ' ', $userid;
        push @filtered, $answer;
    }

As long as I do just this, the second find_one delivers a result. If, however, I do this:

    for my $answer (@{$answers}) {
        say $answer->{author}->{id}, ' ', $userid;
        if ($answer->{author}->{id} == $userid) {
        }
        push @filtered, $answer;
    }

I get the output from above (where the second dumper yields no return from the find_one. It's insane - the if-clause containing the numeric eq causes the second find_one to fail! This is also the grep body in the intended code.

What's going on here? How can this have possibly any effect on the MongoDB methods?

Upvotes: 2

Views: 64

Answers (1)

choroba
choroba

Reputation: 241938

Using the numeric comparison operator == numifies the value, but it's probably too large to fit into an integer and becomes a float. It can also just become an integer and lose double quotes when serialized to JSON or similar format. Using eq instead of == keeps the value unchanged.

Upvotes: 3

Related Questions