2

Is it possible to use advanced MongoDB operators such as Array Update Operators within a Loopback 4 application?

nflaig
  • 673
  • 1
  • 8
  • 15

1 Answers1

2

Hello from the LoopBack team

We don't have a first-class support for executing custom MongoDB operators yet.

I see two possible solution:

  • Execute such update command as a custom command. Unfortunately, this is not as straightforward as I would like it to be, see the GitHub issue loopback-next#3342 for more details. The current workaround is to call connector's execute method and manually convert it from callback-style to async/promise style.

    Example code based on https://github.com/strongloop/loopback-next/issues/2807#issuecomment-487590905:

    const repo = // obtain the repository instance, e.g. via @inject()
    const result = await new Promise((resolve, reject) => {
      repo.dataSource.connector.execute('MyCollection', 'update', {
        $push: {
          tags: ['new tag'] 
        }
      ],
      (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
    });
    
  • Enhance LoopBack's Repository implementations to allow MongoDB update operators in the patch data. This feature is already supported by LoopBack 3 but we haven't exposed it in LoopBack 4 yet. Can you open a new GitHub issue please?

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99
  • I created an issue on GitHub [#4379](https://github.com/strongloop/loopback-next/issues/4379) – nflaig Jan 07 '20 at 17:31