| 1 |
NAME |
| 2 |
|
| 3 |
Catmandu::Store::MongoDB - A searchable store backed by MongoDB |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
# On the command line |
| 8 |
$ catmandu import -v JSON --multiline 1 to MongoDB --database_name bibliography --bag books < books.json |
| 9 |
$ catmandu export MongoDB --database_name bibliography --bag books to YAML |
| 10 |
$ catmandu count MongoDB --database_name bibliography --bag books --query '{"PublicationYear": "1937"}' |
| 11 |
$ catmandu count MongoDB --database_name bibliography --bag books --query '{"Author": "Jones"}' --sort '{"PublicationYear":1}' |
| 12 |
|
| 13 |
# In perl |
| 14 |
use Catmandu::Store::MongoDB; |
| 15 |
|
| 16 |
my $store = Catmandu::Store::MongoDB->new(database_name => 'test'); |
| 17 |
|
| 18 |
my $obj1 = $store->bag->add({ name => 'Patrick' }); |
| 19 |
|
| 20 |
printf "obj1 stored as %s\n" , $obj1->{_id}; |
| 21 |
|
| 22 |
# Force an id in the store |
| 23 |
my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' }); |
| 24 |
|
| 25 |
my $obj3 = $store->bag->get('test123'); |
| 26 |
|
| 27 |
$store->bag->delete('test123'); |
| 28 |
|
| 29 |
$store->bag->delete_all; |
| 30 |
|
| 31 |
# All bags are iterators |
| 32 |
$store->bag->each(sub { ... }); |
| 33 |
$store->bag->take(10)->each(sub { ... }); |
| 34 |
|
| 35 |
# Search |
| 36 |
my $hits = $store->bag->search(query => '{"name":"Patrick"}'); |
| 37 |
my $hits = $store->bag->search(query => '{"name":"Patrick"}' , sort => { age => -1} ); |
| 38 |
my $hits = $store->bag->search(query => {name => "Patrick"} , start => 0 , limit => 100); |
| 39 |
my $hits = $store->bag->search(query => {name => "Patrick"} , fields => {_id => 0, name => 1}); |
| 40 |
|
| 41 |
my $next_page = $hits->next_page; |
| 42 |
my $hits = $store->bag->search(query => '{"name":"Patrick"}' , page => $next_page); |
| 43 |
|
| 44 |
my $iterator = $store->bag->searcher(query => {name => "Patrick"}); |
| 45 |
my $iterator = $store->bag->searcher(query => {name => "Patrick"}, fields => {_id => 0, name => 1}); |
| 46 |
|
| 47 |
# Catmandu::Store::MongoDB supports CQL... |
| 48 |
my $hits = $store->bag->search(cql_query => 'name any "Patrick"'); |
| 49 |
|
| 50 |
DESCRIPTION |
| 51 |
|
| 52 |
A Catmandu::Store::MongoDB is a Perl package that can store data into |
| 53 |
MongoDB databases. The database as a whole is called a 'store'. |
| 54 |
Databases also have compartments (e.g. tables) called Catmandu::Bag-s. |
| 55 |
|
| 56 |
CONFIGURATION |
| 57 |
|
| 58 |
database_name |
| 59 |
|
| 60 |
MongoDB database name. |
| 61 |
|
| 62 |
estimate_count |
| 63 |
|
| 64 |
Use a faster estimated collection document count if true. |
| 65 |
|
| 66 |
All other options are passed on to the MongoDB client. |
| 67 |
|
| 68 |
METHODS |
| 69 |
|
| 70 |
new(database_name => $name, %connection_opts) |
| 71 |
|
| 72 |
new(database_name => $name , bags => { data => { cql_mapping => |
| 73 |
$cql_mapping } }) |
| 74 |
|
| 75 |
Create a new Catmandu::Store::MongoDB store with name $name. Optionally |
| 76 |
provide connection parameters (see MongoDB::MongoClient for possible |
| 77 |
options). |
| 78 |
|
| 79 |
The store supports CQL searches when a cql_mapping is provided. This |
| 80 |
hash contains a translation of CQL fields into MongoDB searchable |
| 81 |
fields. |
| 82 |
|
| 83 |
# Example mapping |
| 84 |
$cql_mapping = { |
| 85 |
indexes => { |
| 86 |
title => { |
| 87 |
op => { |
| 88 |
'any' => 1 , |
| 89 |
'all' => 1 , |
| 90 |
'=' => 1 , |
| 91 |
'<>' => 1 , |
| 92 |
'exact' => {field => [qw(mytitle.exact myalttitle.exact)]} |
| 93 |
} , |
| 94 |
sort => 1, |
| 95 |
field => 'mytitle', |
| 96 |
cb => ['Biblio::Search', 'normalize_title'] |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
The CQL mapping above will support for the 'title' field the CQL |
| 102 |
operators: any, all, =, <> and exact. |
| 103 |
|
| 104 |
The 'title' field will be mapped into the MongoDB field 'mytitle', |
| 105 |
except for the 'exact' operator. In case of 'exact' both the |
| 106 |
'mytitle.exact' and 'myalttitle.exact' fields will be searched. |
| 107 |
|
| 108 |
The CQL mapping allows for sorting on the 'title' field. If, for |
| 109 |
instance, we would like to use a special MongoDB field for sorting we |
| 110 |
could have written "sort => { field => 'mytitle.sort' }". |
| 111 |
|
| 112 |
The CQL has an optional callback field 'cb' which contains a reference |
| 113 |
to subroutines to rewrite or augment the search query. In this case, in |
| 114 |
the Biblio::Search package contains a normalize_title subroutine which |
| 115 |
returns a string or an ARRAY of string with augmented title(s). E.g. |
| 116 |
|
| 117 |
package Biblio::Search; |
| 118 |
|
| 119 |
sub normalize_title { |
| 120 |
my ($self,$title) = @_; |
| 121 |
# delete all bad characters |
| 122 |
my $new_title =~ s{[^A-Z0-9]+}{}g; |
| 123 |
$new_title; |
| 124 |
} |
| 125 |
|
| 126 |
1; |
| 127 |
|
| 128 |
bag($name) |
| 129 |
|
| 130 |
Create or retieve a bag with name $name. Returns a Catmandu::Bag. |
| 131 |
|
| 132 |
client |
| 133 |
|
| 134 |
Return the MongoDB::MongoClient instance. |
| 135 |
|
| 136 |
database |
| 137 |
|
| 138 |
Return a MongoDB::Database instance. |
| 139 |
|
| 140 |
drop |
| 141 |
|
| 142 |
Delete the store and all it's bags. |
| 143 |
|
| 144 |
transaction(\&sub) |
| 145 |
|
| 146 |
Execute $sub within a transaction. See Catmandu::Transactional. |
| 147 |
|
| 148 |
Note that only MongoDB databases with feature compatibility >= 4.0 and |
| 149 |
in a replica set have support for transactions. See |
| 150 |
https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/#view-fcv |
| 151 |
and |
| 152 |
https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/ |
| 153 |
for more info. |
| 154 |
|
| 155 |
Search |
| 156 |
|
| 157 |
Search the database: see Catmandu::Searchable and |
| 158 |
Catmandu::CQLSearchable. This module supports an additional search |
| 159 |
parameter: |
| 160 |
|
| 161 |
- fields => { <field> => <0|1> } : limit fields to return from a query (see L<MongoDB Tutorial|https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/>) |
| 162 |
|
| 163 |
SEE ALSO |
| 164 |
|
| 165 |
Catmandu::Bag, Catmandu::CQLSearchable, Catmandu::Droppable, |
| 166 |
Catmandu::Transactional, MongoDB::MongoClient |
| 167 |
|
| 168 |
AUTHOR |
| 169 |
|
| 170 |
Nicolas Steenlant, <nicolas.steenlant at ugent.be> |
| 171 |
|
| 172 |
CONTRIBUTORS |
| 173 |
|
| 174 |
Johann Rolschewski, <jorol at cpan.org> |
| 175 |
|
| 176 |
LICENSE AND COPYRIGHT |
| 177 |
|
| 178 |
This program is free software; you can redistribute it and/or modify it |
| 179 |
under the terms of either: the GNU General Public License as published |
| 180 |
by the Free Software Foundation; or the Artistic License. |
| 181 |
|
| 182 |
See http://dev.perl.org/licenses/ for more information. |
| 183 |
|