<commit_before>#`{{
  Testing
    collection.find()                   Query database
      Implicit AND selection            Find with more fields
      $gt, $gte                         Greater than and friends
      $lt, $lte                         Less than and friends
      $ne                               not equal
      $in, $nin, $or                    or
      $not                              not
      $regex                            using BSON::Regex
}}

BEGIN { @*INC.unshift( './t' ) }
use Test-support;

use v6;
use Test;
use MongoDB::Collection;
use BSON::Regex;

my MongoDB::Collection $collection = get-test-collection( 'test', 'testf');

# Fill: d0/n99, d2/n97, d4/n95, d6/n93 ... d96/n3, d98/n1
#
for ^100 -> $i, $j {
  my %d = %( code1 => 'd' ~ $i, code2 => 'n' ~ (100 - $j));
  $collection.insert(%d);
}

#show-documents( $collection, {}, {_id => 0});

my MongoDB::Connection $connection = $collection.database.connection;
my Hash $version = $connection.version;
say "V: ", $version.perl;

#-----------------------------------------------------------------------------
my Hash $query = %( code1 => 'd1', code2 => 'n1');
my MongoDB::Cursor $cursor = $collection.find($query);
is $cursor.count, 0, 'There are no documents';

#-----------------------------------------------------------------------------
# Release below 3 gets errors for $eq
#
if 1 {
  $query = %( code1 => {'$eq' => 'd80'},
              code2 => {'$eq' => 'n19'}
            );

  if $version<release1> < 3 {
    $cursor = $collection.find($query);
    my $c = $cursor.count;

    CATCH {
      when X::MongoDB::Cursor {
        ok .message ~~ ms/'exception:' 'invalid' 'operator:' '$eq'/,
           'exception: invalid operator: $eq';
      }

      default {
        say .perl;
      }
    }
  }

  else {
    $cursor = $collection.find($query);
    is $cursor.count, 1, 'There is 1 document';
  }
}

#-----------------------------------------------------------------------------
$query<code1> = %('$gt' => 'd50');
$query<code2>:delete;
$cursor = $collection.find($query);

# d6, d8 are gt d50! => d6, d8, d52, d54, ...d98 = 26 docs
#
is $cursor.count, 26, '26 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$gte' => 'd50');
$cursor = $collection.find($query);

# d6, d8 are gt d50! => d6, d8, d50, d52, d54, ...d98 = 27 docs
#
is $cursor.count, 27, '27 documents';

#-----------------------------------------------------------------------------
$query<code1>:delete;
$query<code2> = %('$lt' => 'n51');
$cursor = $collection.find($query);

# n49, n47, n45, .., n1 = 23 docs
#
is $cursor.count, 23, '23 documents';

#-----------------------------------------------------------------------------
$query<code2> = %('$lte' => 'n51');
$cursor = $collection.find($query);

# n51, n49, n47, n45, .., n1 = 23 docs
#
is $cursor.count, 24, '24 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$gt' => 'd42');
$query<code2> = %('$gte' => 'n51');
$cursor = $collection.find($query);

# d6/n93, d8/n91, d44/n55, d46/n53, d48/n51, d90/n9, d92/n7 = 7 docs
#
is $cursor.count, 7, '7 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$ne' => 'd42');
$query<code2> = %('$ne' => 'n99');
$cursor = $collection.find($query);

is $cursor.count, 48, '48 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$in' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
$cursor = $collection.find($query);

# 2 documents because of d96/n3 and d98/n1
#
is $cursor.count, 2, '2 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$in' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
#say %('$or' => [$query]).perl;
$cursor = $collection.find(%('$or' => [$query]));

# 5 documents because of d96/n3 and d98/n1 overlap
# ?????????????
is $cursor.count, 2, '2 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$nin' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
$cursor = $collection.find($query);

# 1 documents because of d96/n3 and d98/n1 are not in code1, n11 is.
#
is $cursor.count, 1, '1 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$not' => %('$gte' => 'd98'));
$query<code2>:delete;
$cursor = $collection.find($query);

# All but one = 49 docs
#
is $cursor.count, 49, '49 documents';
<commit_msg>doc test. bugfix of $or test<commit_after>#`{{
  Testing
    collection.find()                   Query database
      Implicit AND selection            Find with more fields
      $eq, $gt, $gte                    Equal, Greater than and friends
      $lt, $lte                         Less than and friends
      $ne                               not equal
      $in, $nin                         any/none of the values
      $or                               or
      $not                              not
      $regex                            using BSON::Regex
}}

BEGIN { @*INC.unshift( './t' ) }
use Test-support;

use v6;
use Test;
use MongoDB::Collection;
use BSON::Regex;

my MongoDB::Collection $collection = get-test-collection( 'test', 'testf');

# Fill: d0/n99, d2/n97, d4/n95, d6/n93 ... d96/n3, d98/n1
#
for ^100 -> $i, $j {
  my %d = %( code1 => 'd' ~ $i, code2 => 'n' ~ (100 - $j));
  $collection.insert(%d);
}

#show-documents( $collection, {}, {_id => 0});

my MongoDB::Connection $connection = $collection.database.connection;
my Hash $version = $connection.version;
say "V: ", $version.perl;

#-----------------------------------------------------------------------------
# Implicit $and
#
my Hash $query = %( code1 => 'd1', code2 => 'n1');
my MongoDB::Cursor $cursor = $collection.find($query);
is $cursor.count, 0, 'Implicit $and, There are no documents';

#-----------------------------------------------------------------------------
# $eq. Mongod release below 3 returns errors for $eq
#
if 1 {
  $query = %( code1 => {'$eq' => 'd80'},
              code2 => {'$eq' => 'n19'}
            );

  if $version<release1> < 3 {
    $cursor = $collection.find($query);
    my $c = $cursor.count;

    CATCH {
      when X::MongoDB::Cursor {
        ok .message ~~ ms/'exception:' 'invalid' 'operator:' '$eq'/,
           'exception: invalid operator: $eq';
      }

      default {
        say .perl;
      }
    }
  }

  else {
    $cursor = $collection.find($query);
    is $cursor.count, 1, 'There is 1 document';
  }
}

#-----------------------------------------------------------------------------
# $gt,
#
$query<code1> = %('$gt' => 'd50');
$query<code2>:delete;
$cursor = $collection.find($query);

# d6, d8 are gt d50! => d6, d8, d52, d54, ...d98 = 26 docs
#
is $cursor.count, 26, 'code1 $gt d50, 26 documents';

#-----------------------------------------------------------------------------
# $gte
#
$query<code1> = %('$gte' => 'd50');
$cursor = $collection.find($query);

# d6, d8 are gt d50! => d6, d8, d50, d52, d54, ...d98 = 27 docs
#
is $cursor.count, 27, 'code1 $gte d50, 27 documents';

#-----------------------------------------------------------------------------
# $lt
#
$query<code1>:delete;
$query<code2> = %('$lt' => 'n51');
$cursor = $collection.find($query);

# n49, n47, n45, .., n1 = 23 docs
#
is $cursor.count, 23, 'code2 $lt n51, 23 documents';

#-----------------------------------------------------------------------------
# $lte
#
$query<code2> = %('$lte' => 'n51');
$cursor = $collection.find($query);

# n51, n49, n47, n45, .., n1 = 23 docs
#
is $cursor.count, 24, 'code2 $lte n51, 24 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$gt' => 'd42');
$query<code2> = %('$gte' => 'n51');
$cursor = $collection.find($query);

# d6/n93, d8/n91, d44/n55, d46/n53, d48/n51, d90/n9, d92/n7 = 7 docs
#
is $cursor.count, 7, 'code1 $gt d42 and code2 $gte n51, 7 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$ne' => 'd42');
$query<code2> = %('$ne' => 'n99');
$cursor = $collection.find($query);

is $cursor.count, 48, 'code1 $ne d42 and code2 ne n99, 48 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$in' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
$cursor = $collection.find($query);

# 2 documents because of d96/n3 and d98/n1
#
is $cursor.count,
   2,
   'code1 $in [<d42 d64 d96 d98>] and code2 $in [<n1 n3 n11>], 2 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$in' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
#say %('$or' => [$query]).perl;
$cursor = $collection.find( %(
    '$or' => [
      { code1 => {'$in' => [<d42 d64 d96 d98>]}},
      { code2 => {'$in' => [<n1 n3 n11>]}}
    ]
  )
);

# 5 documents because of d96/n3 and d98/n1 overlap
#
is $cursor.count,
   5,
   '$or [{code1 => {$in => []},{code2 => {$in => []}], 2 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$nin' => [<d42 d64 d96 d98>]);
$query<code2> = %('$in' => [<n1 n3 n11>]);
$cursor = $collection.find($query);

# 1 documents because of d96/n3 and d98/n1 are not in code1, n11 is.
#
is $cursor.count, 1, '1 documents';

#-----------------------------------------------------------------------------
$query<code1> = %('$not' => %('$gte' => 'd98'));
$query<code2>:delete;
$cursor = $collection.find($query);

# All but one = 49 docs
#
is $cursor.count, 49, '49 documents';

{'fraction_non_alphanumeric': 0.36568228105906314, 'fraction_numerical': 0.056415478615071284, 'mean_word_length': 1.477547931382442, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4387396', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3174, 'n_words': 829}