2

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?

2
  • 1
    What happens if you use eq instead of ==? Commented Jul 26, 2019 at 7:09
  • @choroba unbelievably, everything starts to work as intended! But ... WHY??? Commented Jul 26, 2019 at 7:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Thank you for this catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.