Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to execute raw MongoDB queries #2807

Closed
sureshkodur opened this issue Apr 29, 2019 · 8 comments
Closed

How to execute raw MongoDB queries #2807

sureshkodur opened this issue Apr 29, 2019 · 8 comments
Labels
Repository Issues related to @loopback/repository package spike

Comments

@sureshkodur
Copy link

sureshkodur commented Apr 29, 2019

Description / Steps to reproduce / Feature proposal

How to use raw queries like aggregation on mongodb. Is there any solution ?

Current Behavior

Expected Behavior

I want to use raw queries or other models. For example, Customer model, and Products model. I need to write aggregate query like

db.LoanRequestMappings.aggregate([
    {
        $lookup:{
            .....   
        }
    },
    {   $unwind:"$data" },
    {
        $match:{
            ...
        }
    },
    {
        $lookup:{
            ... 
        }
    },
    {   $unwind:"$LoanRepayment" },
    { $project : { _id: 0} },
    { $out : "tempData" }
])

on product model while getting customer model.

Please help.

See Reporting Issues for more tips on writing good issues

@bajtos
Copy link
Member

bajtos commented Apr 29, 2019

You can execute raw queries using the recently introduced Repository method execute, see #2681. This works great for SQL connectors.

Based on the code we have in loopback-datasource-juggler and loopback-connector-mongodb, I suspect the execute method will not work for MongoDB, because the connector requires more than just a command string and args array :(

We need to find a way how to improve LB4 API, MongoDB connector API or both, to make it easy to execute raw MongoDB commands.

Until that happens, you can call MongoDB connector's execute method directly.

const repo = // obtain the repository instance, e.g. via @inject()
const result = await new Promise((resolve, reject) => {
  repo.dataSource.connector.execute('LoanRequestMappings', 'aggregate', [
    {
        $lookup:{
            .....   
        }
    },
    {   $unwind:"$data" },
    {
        $match:{
            ...
        }
    },
    {
        $lookup:{
            ... 
        }
    },
    {   $unwind:"$LoanRepayment" },
    { $project : { _id: 0} },
    { $out : "tempData" }
  ],
  (err, data) => {
    if (err) reject(err);
    else resolve(data);
  });
});

@bajtos bajtos added spike Repository Issues related to @loopback/repository package labels Apr 29, 2019
@bajtos bajtos changed the title Raw mongo queries How to execute raw MongoDB queries Apr 29, 2019
@dougal83
Copy link
Contributor

dougal83 commented Apr 30, 2019 via email

@sureshkodur
Copy link
Author

@bajtos said dataSource.connector.execute will work. But it giving undefined connector. See the second image. I tried #2021 . But it is also not working. it says 500 TypeError: Cannot read property 'settings' of undefined at MongoDB.collectionName . Can you help me

@dougal83
Copy link
Contributor

@sureshkodur Sorry I didn't see the images via email earlier. Maybe screen dumps are less than optimal..

It looks like you're falling foul of typescripts type checking. I think you'll need to provide a repo in order to get help with this.

@dougal83
Copy link
Contributor

dougal83 commented May 1, 2019

@sureshkodur Sorry I can't help you based on the code you've shown. Please bear in mind that I'm just learning LB4 too. Without the ability to run your code I'm at a loss. Hopefully someone else can come along and help.

@bajtos
Copy link
Member

bajtos commented Jun 14, 2019

@sureshkodur is this issue still relevant? Please create a small application we can use to reproduce the problem, see https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-4x-bugs

@bajtos
Copy link
Member

bajtos commented Jul 12, 2019

I opened a new issue to describe the more generic feature we are looking for - the ability to execute raw MongoDB queries. See #3342

It would be great if you can up-vote the issue by adding a 👍 reaction (not a comment please, see https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/), that way we will know how many people are asking for this particular feature.

@VigneshMeem
Copy link

You can execute raw queries using the recently introduced Repository method execute, see #2681. This works great for SQL connectors.

Based on the code we have in loopback-datasource-juggler and loopback-connector-mongodb, I suspect the execute method will not work for MongoDB, because the connector requires more than just a command string and args array :(

We need to find a way how to improve LB4 API, MongoDB connector API or both, to make it easy to execute raw MongoDB commands.

Until that happens, you can call MongoDB connector's execute method directly.

const repo = // obtain the repository instance, e.g. via @inject()
const result = await new Promise((resolve, reject) => {
  repo.dataSource.connector.execute('LoanRequestMappings', 'aggregate', [
    {
        $lookup:{
            .....   
        }
    },
    {   $unwind:"$data" },
    {
        $match:{
            ...
        }
    },
    {
        $lookup:{
            ... 
        }
    },
    {   $unwind:"$LoanRepayment" },
    { $project : { _id: 0} },
    { $out : "tempData" }
  ],
  (err, data) => {
    if (err) reject(err);
    else resolve(data);
  });
});

To resolve as array :
> else resolve(data.toArray());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Repository Issues related to @loopback/repository package spike
Projects
None yet
Development

No branches or pull requests

4 participants