Page Contents

Home > @loopback/repository > DefaultCrudRepository > execute

DefaultCrudRepository.execute() method

Execute a MongoDB command.

**WARNING:** In general, it is always better to perform database actions through repository methods. Directly executing MongoDB commands may lead to unexpected results and other issues.

Signature:

execute(collectionName: string, command: string, ...parameters: PositionalParameters): Promise<AnyObject>;

Parameters

Parameter Type Description
collectionName string The name of the collection to execute the command on.
command string The command name. See [Collection API docs](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html) for the list of commands supported by the MongoDB client.
parameters PositionalParameters Command parameters (arguments), as described in MongoDB API docs for individual collection methods.

Returns:

Promise<AnyObject>

A promise which resolves to the command output as returned by the database driver.

Example

const result = await repo.execute('MyCollection', 'aggregate', [
  {$lookup: {
    // ...
  }},
  {$unwind: '$data'},
  {$out: 'tempData'}
]);