Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@atelierfabien/loopback-connector-mongodb

fabien39MIT4.0.6

The official MongoDB connector for the LoopBack framework.

connector, datasource, loopback, strongloop, mongodb

readme

loopback-connector-mongodb

The official MongoDB connector for the LoopBack framework.

Please see the full documentation at loopback.io.

Installation

In your application root directory, enter this command to install the connector:

npm install loopback-connector-mongodb --save

This installs the module from npm and adds it as a dependency to the application's package.json file.

If you create a MongoDB data source using the data source generator as described below, you don't have to do this, since the generator will run npm install for you.

Creating a MongoDB data source

Use the Data source generator to add a MongoDB data source to your application.
The generator will prompt for the database server hostname, port, and other settings required to connect to a MongoDB database. It will also run the npm install command above for you.

The entry in the application's /server/datasources.json will look like this:

"mydb": {
  "host": "myserver",
  "port": 27017,
  "url":  "",
  "database": "test",
  "password": "mypassword",
  "name": "mydb",
  "user": "me",
  "authSource" : "admin",
  "connector": "mongodb"
}

Edit datasources.json to add any other additional properties that you require.

Connection properties

Property Type   Description
connector String Connector name, either "loopback-connector-mongodb" or "mongodb".
database String Database name
host String Database host name
password String Password to connect to database
port Number Database TCP port
url String Connection URL of form mongodb://user:password@host/db. Overrides other connection settings (see below).
user String Username to connect to database
authSource String Authentification database name (optional). Usually "admin" value.

If you run a MongoDB with authentification (Docker's example here), you need to specify which database to authenticate against. More details can be found in MongoDB documentation on Authentification Methods. The default value is usually "admin", like in the official docker image.

NOTE: In addition to these properties, you can use additional Single Server Connection parameters supported by node-mongodb-native.

Additional properties

  • allowExtendedOperators: Set to true to enable using MongoDB operators such as $currentDate, $inc, $max, $min, $mul, $rename, $setOnInsert, $set, $unset, $addToSet, $pop, $pullAll, $pull, $pushAll, $push, and $bit. Default is false.
  • enableGeoIndexing: Set to true to enable 2dsphere indexing for model properties of type GeoPoint. This allows for indexed near queries. Default is false.
  • lazyConnect:
    • Default is false.
    • If set to true, the database instance will not be attached to the datasource and the connection is deferred.
    • It will try to establish the connection automatically once users hit the endpoint. If the mongodb server is offline, the app will start, however, the endpoints will not work.
  • disableDefaultSort: Set to true to disable the default sorting behavior on id column, this will help performance using indexed columns available in mongodb.
  • collation: Specify language-specific rules for string comparison, such as rules for lettercase and accent marks. See MongdoDB documentation for details. It can also be used to create case insensitive indexes.

Setting the url property in datasource.json

You can set the url property to a connection URL in datasources.json to override individual connection parameters such as host, user, and password.

Additionally, you can override the global url property in environment-specific data source configuration files, for example for production in datasources.production.json, and use the individual connection parameters host, user, password, and port. To do this, you must set url to false, null, or “” (empty string). If you set url to undefined or remove the url property altogether, the override will not work.

For example, for production, use datasources.production.json as follows (for example) to overide the url setting in `datasources.json:

"mydb": {
  "host": "myserver",
  "port": 27017,
  "url":  false,
  "database": "test",
  "password": "mypassword",
  "name": "mydb",
  "user": "me",
  "connector": "mongodb"  
}

For more information on setting data source configurations for different environments, see Environment-specific configuration.

Security Considerations

MongoDB Driver allows the $where operator to pass in JavaScript to execute on the Driver which can be used for NoSQL Injection. See MongoDB: Server-side JavaScript for more on this MongoDB feature.

To protect users against this potential vulnerability, LoopBack will automatically remove the $where and mapReduce operators from a query before it's passed to the MongoDB Driver. If you need to use these properties from within LoopBack programatically, you can disable the sanitization by passing in an options object with disableSanitization property set to true.

Example:

Post.find(
    {where: {$where: 'function() { /*JS function here*/}'}},
    {disableSanitization: true},
    (err, p) => {
        // code to handle results / error.
    }
);

Type mappings

See LoopBack types for details on LoopBack's data types.

LoopBack to MongoDB types

Type conversion is mainly handled by Mongodb. See 'node-mongodb-native' for details.

Customizing MongoDB configuration for tests/examples

By default, examples and tests from this module assume there is a MongoDB server instance running on localhost at port 27017.

To customize the settings, you can drop in a .loopbackrc file to the root directory of the project or the home folder.

Note: Tests and examples in this project configure the data source using the deprecated '.loopbackrc' file method, which is not suppored in general. For information on configuring the connector in a LoopBack application, please refer to loopback.io.

The .loopbackrc file is in JSON format, for example:

{
    "dev": {
        "mongodb": {
            "host": "127.0.0.1",
            "database": "test",
            "user": "youruser",
            "password": "yourpass",
            "port": 27017
        }
    },
    "test": {
        "mongodb": {
            "host": "127.0.0.1",
            "database": "test",
            "user": "youruser",
            "password": "yourpass",
            "port": 27017
        }
    }
}

Note: user/password is only required if the MongoDB server has authentication enabled. "authSource" should be used if you cannot login to your database using your credentials.

Running tests

Own instance

If you have a local or remote MongoDB instance and would like to use that to run the test suite, use the following command:

  • Linux
    MONGODB_HOST=<HOST> MONGODB_PORT=<PORT> MONGODB_DATABASE=<DATABASE> CI=true npm test
  • Windows
    SET MONGODB_HOST=<HOST> SET MONGODB_PORT=<PORT> SET MONGODB_DATABASE=<DATABASE> SET CI=true npm test

Docker

If you do not have a local MongoDB instance, you can also run the test suite with very minimal requirements.

  • Assuming you have Docker installed, run the following script which would spawn a MongoDB instance on your local:
    source setup.sh <HOST> <PORT> <DATABASE>
    where <HOST>, <PORT> and <DATABASE> are optional parameters. The default values are localhost, 27017 and testdb respectively.
  • Run the test:
    npm test

Leak detection

Tests run for 100 iterations by default, but can be increased by setting the env var ITERATIONS.

make leak-detection # run 100 iterations (default)

or

ITERATIONS=1000 make leak-detection # run 1000 iterations

Running benchmarks

Benchmarks must be run on a Unix-like operating system.

make benchmarks

The results will be output in ./benchmarks/results.md.

strictObjectIDCoercion flag

In version 1.17.0, the id of string type is being converted to ObjectID, when the string length is 12 or 24 and has the format of an ObjectID i.e /^[0-9a-fA-F]{24}$/. To avoid this issue, the strictObjectIDCoercion flag should be set to true in the model-definition file. It is also possible to enable this flag on a per method bases by passing it in as part of the options object.

model-definition.js

{
  "name": "myModelName",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
...
}

boot-script.js

'use strict';
var util = require('util');

module.exports = function(app) {
  var db = app.dataSources.mongoDs;
  var myModelName = app.models.myModelName;

  db.automigrate(function(err) {
    if (err) throw err;
    console.log('Automigrate complete');

    myModelName.create([{
      id: '59460487e9532ae90c324b59',
      name: 'Bob',
    }, {
      id: '59460487e9532ae90c324b5a',
      name: 'Sam',
    }, {
      id: '420',
      name: 'Foo',
      age: 1,
    }, {
      id: '21',
      name: 'Bar',
    }], function(err, result) {
      if (err) throw err;
      console.log('\nCreated instances of myModelName: ' + util.inspect(result, 4));

      myModelName.find({where: {id: {inq: ['59460487e9532ae90c324b59',
        '59460487e9532ae90c324b5a']}}},
      function(err, result) {
        if (err) throw err;
        console.log('\nFound instance with inq: ' + util.inspect(result, 4));
      });
    });
  });
};

Per method basis

myModelName.find(
  {where: {id: {inq: ['59460487e9532ae90c324b59', '59460487e9532ae90c324b5a']}}},
  {strictObjectIDCoercion: true},
  function(err, result) {
    // ... 
  }
)

Release notes

  • 1.1.7 - Do not return MongoDB-specific _id to client API, except if specifically specified in the model definition

changelog

2018-11-06, Version 4.0.0

  • Use new url parser by default (#462) (Hugo Da Roit)

  • remove the infinite inspect (#480) (Janny)

  • Add case insensitive indexes support (maxim.sharai)

2018-10-23, Version 3.9.0

  • support decimal128 (#475) (Janny)

  • Added "authSource" in doc connection properties (Rémi AUGUSTE)

  • Convert embedded binary properties to buffer (ntsekouras)

  • Convert projection fields option to object (Dimitris)

2018-09-19, Version 3.8.0

  • fix performance issues on count #464 (Clément)

  • feat: allow methods to pass strictObjectIDCoercion (virkt25)

2018-09-14, Version 3.7.1

  • fix: map new names to old for connector hooks (virkt25)

2018-09-12, Version 3.7.0

  • update deprecated mongo driver commands (Hugo Da Roit)

  • Remove hard dependency of memwatch-next (Raymond Feng)

  • Add support for protocol to be 'monogodb+srv' (Raymond Feng)

2018-08-15, Version 3.6.0

  • docs: update with security consideration section (virkt25)

  • fix: sanitize query by default (virkt25)

  • change count to countDocuments (Rahmat Nugraha)

  • add useNewUrlParser on validOptionNames (Rahmat Nugraha)

  • Dedicated Model for testing disableDefaultSort (HugoPoi)

  • Add disableDefaultSort in README (HugoPoi)

  • Add settings disableDefaultSort for find method (HugoPoi)

2018-07-23, Version 3.5.0

  • chore: drop node 4 and update deps (Taranveer Virk)

  • [WebFM] cs/pl/ru translation (candytangnb)

2018-06-05, Version 3.4.4

  • Fields projection fix (#436) (John Gonyo)

2018-04-06, Version 3.4.3

  • update bson version (Diana Lau)

2018-03-23, Version 3.4.2

  • chore:update CODEOWNERS (Diana Lau)

  • Prioritize db url (Dimitris)

  • CODEOWNERS: add nitro404 (Miroslav Bajtoš)

2018-01-19, Version 3.4.1

  • fix: allow db name to be parsed from url (Raymond Feng)

2018-01-19, Version 3.4.0

  • upgrade to mongodb driver 3.x (Raymond Feng)

  • Alias find as findById (jannyHou)

2017-12-04, Version 3.3.1

  • Switch to bson.ObjectID (#401) (Kevin Delisle)

  • chore: update license (Diana Lau)

2017-10-13, Version 3.3.0

  • update strong-globalize to 3.1.0 (shimks)

  • Create Issue and PR Templates (#386) (Sakib Hasan)

  • Use stalebot on this repo (#383) (Kevin Delisle)

  • Use stalebot on this repo (Kevin Delisle)

  • Add CODEOWNER file (Diana Lau)

2017-07-10, Version 3.2.1

  • Apply feedback (ssh24)

  • Add docs on lazyConnect flag (ssh24)

2017-06-28, Version 3.2.0

  • Remove the hard-coded writeConcern (Raymond Feng)

  • Document strictObjectIDCorecion flag (Loay)

  • Allow different forms of regexp on like/nlike op (ssh24)

  • Require init on mocha args (ssh24)

  • Use buildSort function to sort (ssh24)

  • Add docker setup (#373) (Sakib Hasan)

  • test: use mongodb-3.2 on Travis (#369) (Ryan Graham)

2017-04-17, Version 3.1.0

  • Update connector version (#368) (Sakib Hasan)

  • Replicate issue_template from loopback repo (#350) (siddhipai)

  • Fix buildNearFilter to work with any key depth (#322) (Corentin H)

  • Fix Update when id not found (Loay)

  • Add additional envs for node v4/v6 (#365) (Sakib Hasan)

  • Update node version (ssh24)

  • Reconnect on execute after disconnect (#362) (phairow)

  • update the near query with minDistance test (#361) (Vincent Wen)

  • Fix lazy connect (#360) (phairow)

  • Export the additional functions (#353) (James Cooke)

  • Mongo 3.4 Support/Delete index ‘kind’ property from index options (#335) (Dylan Lundy)

  • Update README.md (Rand McKinney)

2017-02-13, Version 3.0.1

  • Remove invalid options (jannyHou)

  • Add nestedProperty to connectorCapabilities (jannyHou)

  • Update README.md (Rand McKinney)

  • add info on url override (ivy ho)

  • add link for loopback types to mongodb (ivy ho)

  • replace MySQL with MongodDB (ivy ho)

  • Update Readme with Properties (ivy ho)

  • update lB connector version (Loay)

  • Fix replaceById to report err when id not found (Loay Gewily)

2017-01-13, Version 3.0.0

  • Delete extraneous id for replacById (Amir Jafarian)

  • Update paid support URL (Siddhi Pai)

  • Start 3.x + drop support for Node v0.10/v0.12 (siddhipai)

  • Drop support for Node v0.10 and v0.12 (Siddhi Pai)

  • Start the development of the next major version (Siddhi Pai)

  • Update mongodb version (jannyHou)

  • Update README with correct doc links, etc (Amir Jafarian)

  • Ensure inq/nin use array cond value (Fabien Franzen)

  • More ObjectID vs. String handling improvements (Fabien Franzen)

  • Test returned info for #destroy (Fabien Franzen)

  • Test fix for #253 (Fabien Franzen)

  • Fix Copyright, use process.nextTick (Fabien Franzen)

  • Fix all sorts of issues... (Fabien Franzen)

  • Column renaming should be done before extended ops (Ian Zepp)

  • Added support for renaming columns (Ian Zepp)

2016-10-17, Version 1.17.0

  • Remove TEST prefix for env vars (#292) (Simon Ho)

  • Add connectorCapabilities global object (Nick Duffy)

  • Update translation files - round#2 (Candy)

  • Update deps to loopback 3.0.0 RC (Miroslav Bajtoš)

  • Remove conflict (jannyHou)

  • fix maxDistance not supported in geo filter. (Vincent Wen)

  • Use juggler@3 for running the tests (Miroslav Bajtoš)

  • Remove !intl (jannyHou)

  • Refactor (jannyHou)

  • Globalization (jannyHou)

  • Support patches afterwards (jannyHou)

  • Use the latest compatible mongodb (jannyHou)

  • Update URLs in CONTRIBUTING.md (#264) (Ryan Graham)

2016-05-16, Version 1.15.2

  • Update "mongodb" dependency to caret notation (Bram Borggreve)

2016-05-03, Version 1.15.1

  • insert/update copyrights (Ryan Graham)

  • relicense as MIT only (Ryan Graham)

2016-05-03, Version 1.15.0

  • Lazy connect when booting app (juehou)

  • Add support for geoNear queries (Timo Saikkonen)

  • Fix linting errors (Amir Jafarian)

  • Auto-update by eslint --fix (Amir Jafarian)

  • Add eslint infrastructure (Amir Jafarian)

  • Implementation for replace (Amir Jafarian)

  • Upgrade should to 8.0.2 (Simon Ho)

  • Check dataSource.connecting to prevent race conditions (Fabien Franzen)

  • Remove email from AUTHORS (Simon Ho)

  • Update description in README.md (Simon Ho)

  • Clean up package.json (Simon Ho)

  • Update AUTHORS (Simon Ho)

  • Add AUTHORS file (Simon Ho)

  • Use ObjectId as internal storage for id (Raymond Feng)

  • test: fix order of semver arguments (Ryan Graham)

  • use mocha for test script (Ryan Graham)

  • Add more tests for id coercion (Raymond Feng)

2015-12-13, Version 1.13.2

  • Make sure null/undefined id is not coerced (Raymond Feng)

  • Allow runtime configurable test environment (Simon Ho)

  • changed env variable fortest servers (cgole)

2015-11-23, Version 1.13.1

  • Fix the test set up (Raymond Feng)

  • Added mongo port env var (cgole)

  • Add env variable for mongodb server (cgole)

  • Refer to licenses with a link (Sam Roberts)

  • Fix repository field in package.json (Simon Ho)

  • Use strongloop conventions for licensing (Sam Roberts)

  • Enhance coercion of ids with inq/nin operators (Raymond Feng)

  • Return deleted count (Raymond Feng)

2015-08-06, Version 1.13.0

  • Added a setting to enable optimsied findOrCreate method so that connector continues to work with mongodb < 2.6 (Mike Bissett)

  • Fixed up merge conflicted dependencies in package.json (Mike Bissett)

  • Update deps (Raymond Feng)

  • implement optimized findOrCreate (Clark Wang)

  • extract sort document building to method (Clark Wang)

2015-08-04, Version 1.12.0

  • Add regexp operator support (Simon Ho)

  • Enable options.allowExtendedOperators (Fabien Franzen)

  • Enable Model.settings.mongodb.allowExtendedOperators (Fabien Franzen)

  • Update benchmarks (Simon Ho)

2015-07-02, Version 1.11.3

  • Restore data.id to avoid build breaks (Raymond Feng)

2015-06-25, Version 1.11.2

  • Revert "Add a workaround for auth with multiple mongos servers" (Raymond Feng)

2015-06-15, Version 1.11.1

  • Fix the url (Raymond Feng)

  • Replaced ensureIndex() with createIndex() (U-Zyn Chua)

2015-06-05, Version 1.11.0

  • Add a workaround for auth with multiple mongos servers (Raymond Feng)

  • Use custom collection name for migration (Raymond Feng)

2015-06-03, Version 1.10.1

  • Make sure disconnect calls back (Raymond Feng)

2015-06-01, Version 1.10.0

  • Add execute hooks (Raymond Feng)

2015-05-29, Version 1.9.2

  • Update to memwatch-next for node 0.12 compatibility (Raymond Feng)

2015-05-28, Version 1.9.1

  • Update deps (Raymond Feng)

2015-05-28, Version 1.9.0

  • Add options (Raymond Feng)

  • Update README.md (Simon Ho)

  • Add leak detection (Simon Ho)

  • Add benchmarks (Simon Ho)

  • Support ctx.isNewInstance (Miroslav Bajtoš)

  • Update deps (Raymond Feng)

  • Cleanup for returning count on update/delete (Simon Ho)

  • Default to undefined instead of 0 (Simon Ho)

  • Return info object with affected items count (Simon Ho)

  • added doc about allowExtendedOperators (Pasindu De Silva)

  • Make test instructions more meaningful (Simon Ho)

2015-02-20, Version 1.8.0

  • Update deps (Raymond Feng)

  • Re-enable the inclusion tests (Raymond Feng)

2015-02-08, Version 1.7.0

  • Check if result is null (Raymond Feng)

  • Disable extended tests if mongodb version is lower than 2.6 (Raymond Feng)

  • Include tests of persistence hooks from juggler. (Miroslav Bajtoš)

  • Update should to the latest version 4.6.3 (Miroslav Bajtoš)

  • Fix unit-tests on iojs 1.0 (Miroslav Bajtoš)

  • Tidy up the null check (Raymond Feng)

  • Added options to like/nlike operator to allow for regex flags (Andrew Burgess)

2015-01-23, Version 1.6.0

  • Small optimisation on the loop in parseUpdateData (Alexandru Savin)

  • Optimized the user data parser method (Alexandru Savin)

  • Fixed bug where only the first operator was kept and refactored the parsing logic to be reused (Alexandru Savin)

  • Allows array operators (Felipe Figueroa)

  • Renamed datasource config var for exended parameters (Alexandru Savin)

  • set extended operators to true for the tests where they are needed (Alexandru Savin)

  • added configuration switch extended_operators and more update operators (Alexandru Savin)

  • Added tests (Alexandru Savin)

  • Check for valid operators and disregard any other properties (Alexandru Savin)

  • allow DB specific mappings for indexes (Felipe Figueroa)

  • Changed the solution to use indexOf (Alexandru Savin)

  • Give access to all update operators (Alexandru Savin)

  • Enable additional update actions (other than $set) (Alexandru Savin)

2015-01-14, Version 1.5.0

  • Fix the test case (Raymond Feng)

  • Upgrade to mongodb node driver 2.x (Raymond Feng)

  • Fix bad CLA URL in CONTRIBUTING.md (Ryan Graham)

  • test: Use mongodb on localhost under CI (Ryan Graham)

  • test: bump loopback-datasource-juggler version (Ryan Graham)

  • fixing typo (Marc Puig)

2014-10-31, Version 1.4.5

  • Bump version (Raymond Feng)

  • fix misspelled attribute "unique" in /lib/mongodb.js (kai zhu)

  • Fix the test cases (Raymond Feng)

  • Add contribution guidelines (Ryan Graham)

  • Fix the bad usage of for-in loop against array (Raymond Feng)

2014-09-02, Version 1.4.4

  • Bump version (Raymond Feng)

  • Fix mongodb neq mapping (Raymond Feng)

2014-08-27, Version 1.4.3

  • Bump version (Raymond Feng)

  • Add a test case for ping errors (Raymond Feng)

2014-08-20, Version 1.4.2

  • Bump version (Raymond Feng)

  • Add ping() (Raymond Feng)

2014-06-27, Version 1.4.1

  • Bump versions (Raymond Feng)

  • Tidy up order processing (Raymond Feng)

  • Update link to doc (Rand McKinney)

2014-06-23, Version 1.4.0

  • Bump version (Raymond Feng)

  • Add update support (Raymond Feng)

  • Allows custom mapping from model to collection (Raymond Feng)

  • Add more tests (Raymond Feng)

  • Fix id usage in where clause (Raymond Feng)

  • Add a test case to verify update (Raymond Feng)

2014-06-05, Version 1.3.0

  • Fix the test cases (Raymond Feng)

  • Remove peer dependency on datasource-juggler (Miroslav Bajtoš)

2014-05-27, Version 1.2.6

  • Bump version (Raymond Feng)

  • Map id name to _id for ordering (Raymond Feng)

  • Set the default order using id if no order is specified (Raymond Feng)

2014-05-13, Version 1.2.5

  • Bump version (Raymond Feng)

  • Remove unused code (Raymond Feng)

  • Remove the duplicate test (Raymond Feng)

  • Improve the save method with tests (Raymond Feng)

  • Fix mongodb upsert (Raymond Feng)

2014-05-05, Version 1.2.4

  • Bump versions (Raymond Feng)

  • Make sure where object is built for count/destroyAll (Raymond Feng)

2014-05-02, Version 1.2.3

  • Bump version (Raymond Feng)

  • Add a comment for the null value query (Raymond Feng)

  • Fix the test titles (Raymond Feng)

  • Add support for logical operators (Raymond Feng)

2014-04-24, Version 1.2.2

  • Bump version (Raymond Feng)

  • Add id coercion for findById (Raymond Feng)

2014-04-08, Version 1.2.1

  • Bump version (Raymond Feng)

  • Replace old README with link to docs. (Rand McKinney)

  • Update README (Raymond Feng)

  • Fix the conversion for mongodb.Binary (Raymond Feng)

2014-03-18, Version 1.2.0

  • Set default options for background/unique (Raymond Feng)

  • Add indexes/automigrate/autoupdate/debug (Raymond Feng)

2014-02-27, Version 1.1.8

  • Bump version (Raymond Feng)

  • Simplifying the connector-mongodb PR after database-juggler fix (Aurelien Chivot)

  • Do not return MongoDB-specific _id to client API, except if specifically specified in the model definition (Aurelien Chivot)

2014-02-25, Version 1.1.7

  • Bump version (Raymond Feng)

  • Wrap the condition into RegExp for consistency (Raymond Feng)

  • Add like/nlike support for mongodb (Raymond Feng)

  • Fix error handling for create (Raymond Feng)

2014-02-19, Version 1.1.6

  • Bump version (Raymond Feng)

  • Fix for text id object insertion issue (Aurelien Chivot)

  • Update to dual MIT/StrongLoop license (Raymond Feng)

  • Fix the object id conversion to skip 12-byte strings (Raymond Feng)

2014-02-11, Version 1.1.5

  • Fix update (Raymond Feng)

  • Update getTypes (Raymond Feng)

  • Add type/defaultIdType (Raymond Feng)

  • Reformat code (Raymond Feng)

2013-12-16, Version 1.1.4

  • Bump version (Raymond Feng)

  • Rename the model (Raymond Feng)

  • Add a test case (Raymond Feng)

  • Try to convert the id to ObjectID (Raymond Feng)

2013-12-06, Version 1.1.3

  • First release!