简体   繁体   中英

Perl MongoDB API - regular expression in filters

I am working in Perl with a MongoDB. I have a collection with documents that have a big text field that I need to be able to find all rows that contain multiple strings in the field.

So for instance, if this is a database of movie quotes one row would have value:

We must totally destroy all spice production on Arrakis. The Guild and the entire Universe depends on spice. He who can destroy a thing, controls a thing.

I want to be able to match that row with terms "spice", "Arrakis", and "Guild" where ALL of those terms have to be in the text.

My current approach can only achieve matches if the terms provided happen to be in the correct order, ie:

$db->get_collection( 'quotes' )->find( { quote => qr/spice.*Arrakis.*Guild/i } );

That's a match, but

$db->get_collection( 'quotes' )->find( { quote => qr/Guild.*spice.*Arrakis/i } );

is not a match.

If I were working with a SQL database I could do:

... WHERE quote LIKE '%spice%' and quote LIKE '%Arrakis%' and quote LIKE '%Guild%'

but with the MongoDB interface you only get one shot per field.

Is there a way to match multiple words where all are required in one regex, or is there another way to get more than one crack at a field in the MongoDB interface?

One way: A bunch of positive lookahead assertations:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw/say/;
my @tests = ("The Guild demands that the spice must flow from Arrakis",
             "House Atreides will be transported to Arrakis by the Guild.");
for my $test (@tests) {
  if ($test =~ m/^(?=.*spice)
                  (?=.*Guild)
                  (?=.*Arrakis)/x) {
    say "$test: matches";
  } else {
    say "$test: fails";
  }
}

produces:

The Guild demands that the spice must flow from Arrakis: matches

Duke Leto will be transported to Arrakis by the Guild.: fails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM