ProductOpener::Data - methods to create or get the mongoDB client and fetch "data collections" from the MongoDB database;
The module implements the methods required to fetch certain collections from the MongoDB database. The functions used in this module are responsible for executing queries, to get connection to database and also to select the collection required.
The products collection contains a complete document for each product in the OpenFoodFacts database which exposes all available information about the product.
Obsolete products that have been withdrawn from the market have separate collections: products_obsolete
Initializes the data_debug variable that will list parameters and info related to data queries
Determine if we can use off_query backend: - off_query URL needs to be set - no_off_query parameter is not set, or off_query parameter is set - we are not on the producers platform
Determine if we can use cached results: - no_cache parameter is not set - we are not on the producers platform - Cache-Control header is not set to no-cache
This parameter is modified to add debug information.
execute_query()
executes a query on the database.
A query subroutine that performs a query against the database.
The function returns the return value of the query subroutine $sub passed as a parameter to it.
eval { $result = execute_query(sub { return get_products_collection()->query({})->sort({code => 1}); }); }
get_products_collection()
establishes a connection to MongoDB and uses timeout as an argument.
This then selects a collection from within the database.
This method takes parameters in an optional hash reference with the following keys:
Defaults to $ProductOpener::Config::mongodb
This is useful when moving products to another flavour (e.g. from Open Food Facts (database: off) to Open Beauty Facts (database: obf))
If set to a true value, the function returns a collection that contains only obsolete products, otherwise it returns the collection with products that are not obsolete.
Returns a mongoDB collection object.
get_mongodb_client()
gets the MongoDB client.
It first checks if the client already exists and if not,
it creates and configures a new MongoDB::MongoClient.
This method takes in arguments of integer type (user defined timeout in milliseconds). It is optional for this subroutine to have an argument.
Returns $client of type MongoDB::MongoClient object.
Efficiently removes a set of documents
correspond to the _id field
Returns a hash with:
<dl> <dt>removed</dt> <dd>int - number of effectively removed items</dd> <dt>errors</dt> <dd>ref to a list of errors</dd> </dl>