Reputation: 57
I'm evaluating the adoption of Loopback4 for a new project. I studied the tutorial and the documentation and I've to make some test. One of those is related to the capabilities of executing native mongodb queries.
I found a lot of examples related to Loopback v3 but since the structure is quite different (compared to Lb4), they are not reliable. Of course, I'd like to keep the separation among model/repository/datasource as designed by Lb4 and I'd like to mantain the typization. Does anyone as tryed to do the same thing? Every help will be apreciated Thanks a lot
Upvotes: 0
Views: 1965
Reputation: 10795
I am afraid LoopBack 4 does not have any easy-to-use API for executing raw MongoDB queries. We are discussing this feature in the GitHub issue #2807.
Until we implement first-class support, you can call MongoDB connector's execute
method directly as follows:
const repo = // obtain the repository instance, e.g. via @inject()
const result = await new Promise((resolve, reject) => {
repo.dataSource.connector.execute(
'LoanRequestMappings', // collection name
'aggregate', // command to execute
// additional arguments
[
{
$lookup:{
// ...
}
},
{ $unwind:"$data" },
{
$match:{
// ...
}
},
{
$lookup:{
// ...
}
},
{ $unwind:"$LoanRepayment" },
{ $project : { _id: 0} },
{ $out : "tempData" }
],
(err, data) => {
if (err) reject(err);
else resolve(data);
});
});
Upvotes: 2