MongoDB/Perl: find_one doesn't return data after unrelated code
NickName:Perlator Ask DateTime:2019-07-26T15:05:35

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?

Copyright Notice:Content Author:「Perlator」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57214778/mongodb-perl-find-one-doesnt-return-data-after-unrelated-code

More about “MongoDB/Perl: find_one doesn't return data after unrelated code” related questions

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' =&g

Show Detail

Insert into mongodb with perl

I wrote a script to insert items into mongodb #!/usr/bin/perl use strict; use warnings; use MongoDB; use Data::Dumper; my $hostname = "localhost"; my $port = 27017; my $conn = MongoDB::Connectio...

Show Detail

Read MongoDB array into perl and walk data

I am trying to capture an array from my MongoDB database into my Perl script and read each element. This is something that I thought would be simple, but for some dumb reason it is kicking my rear...

Show Detail

`find_one` equivalent in DynamoDB?

I am trying to limit the response of scan operator to just one in DynamoDB. I came across an option, LIMIT but, this doesn't really limit the matching results to one. Rather, it specifies the amoun...

Show Detail

What is difference between findOne and find_one in Mongo?

I'm new in mongodb. Anyone tell me the difference between findOne and find_one query in mongodb.

Show Detail

How to subtract minutes from ISODate() in perl and mongoDB?

Actually, I want to subtract minutes from my execution time field (subtract 5 minutes)... this is my mongodb collection : { "_id" : ObjectId("5c4f11fae2fb4adc98f323ad"), "datetime" : ISOD...

Show Detail

Why is find_one() timing out when find() works just fine using MongoDB?

The code below connects to MongoDB, then attempts to find the specified record, which exists. The collections are very small. client = pymongo.MongoClient("PRIVATE_MONGO_ACCESS_HERE?retryWrite...

Show Detail

Why is find_one() timing out when find() works just fine using MongoDB?

The code below connects to MongoDB, then attempts to find the specified record, which exists. The collections are very small. client = pymongo.MongoClient("PRIVATE_MONGO_ACCESS_HERE?retryWrite...

Show Detail

can't get data from mongodb using perl (get undefined value)

I am trying to get data from mongodb using perl, but I get undefined value for variable $people my $client = MongoDB::MongoClient->new(host => 'mongodb://xxx.xxx.xxx.xxx',port=>27017, us...

Show Detail

find_one MongoDB Ruby Driver

It looks like the current Ruby Mongo Driver 2.0.4 no longer has the find_one method. I can only find it in reference to GridFS. How can I retrieve a single document from Mongo using the official r...

Show Detail