Installation

Icon

The MongoDB connector indirectly uses bson, that requires you to have a standard set of compiler tools on your system. See Installing compiler tools for details.

In your application root directory, enter:

shell
$ npm install loopback-connector-mongodb --save

This will install the module from npm and add it as a dependency to the application's package.json file.

Creating a MongoDB data source 

Use the Data source generator to add a MongoDB data source to your application.  The entry in the application's /server/datasources.json will look like this:

/server/datasources.json
"mydb": {
    "name": "mydb",
    "connector": "mongodb",
}

Edit datasources.json to add other properties that enable you to connect the data source to a MongoDB database.

Properties

PropertyTypeDescription
connectorString

Connector name, either "loopback-connector-mongodb" or "mongodb"

databaseStringDatabase name
debugBooleanIf true, turn on verbose mode to debug database queries and lifecycle.
hostStringDatabase host name or IP address.
passwordStringPassword to connect to database, if required.
portNumberDatabase TCP port
urlString

Connection string URI; see http://docs.mongodb.org/manual/reference/connection-string/.

See Replica set configuration below.

usernameStringUsername to connect to database, if required.
Icon

Username and password are required only if the MongoDB server has authentication enabled.

For example:

/server/datasources.json
{
  "mongodb_dev": { 
    "name": "mongodb_dev",
    "connector": "mongodb",
    "host": "127.0.0.1", 
    "database": "devDB", 
    "username": "devUser", 
    "password": "devPassword", 
    "port": 27017 
  },
  "mongodb_staging": {
    "name": "mongodb_staging",
    "connector": "mongodb",
    "host": "127.0.0.1", 
    "database": "stagingDB", 
    "username": "stagingUser", 
    "password": "stagingPassword", 
    "port": 27017 
  }
}

Using the MongoDB connector

Icon

LoopBack currently does not currently support property mapping for MongoDB; you can customize only collection names. 

Customize the collection name

You might want to customize the collection name for a LoopBack model. It can be done in the model definition JSON file. In the example below, the Post model will be mapped to the PostCollection collection in MongoDB.

/common/models/model.json
{
  "name": "Post",
  "mongodb": {
    "collection": "PostCollection"
  },
  "properties": {
    ...
  }
}

Replica set configuration

The LoopBack MongoDB connector supports the replica set configuration using the MongoDB connection string URI format.  For example, here is a snippet for the data source configuration:

 

/server/datasources.json
{
	“connector": “mongodb”,
	“url”: "mongodb://example1.com,example2.com,example3.com/?readPreference=secondary"
}

About MongoDB _id field

MongoDB uses a specific ID field with BSON ObjectID type, named _id

The MongoDB connector does not expose the MongoDB _id field, to be consistent with other connectors. Instead, it is transparently mapped to the id field, which is declared by default in the model if you do not define any id.

To access the _id property, you must define it explicitly as your model ID, along with its type; For example:

/server/script.js
var ds = app.dataSources.db;
MyModel = ds.createModel('mymodel', {
    _id: { type: ds.ObjectID, id: true }
});

Example with a Number _id :

/server/script.js
MyModel = ds.createModel('mymodel', {
    _id: { type: Number, id: true }
});

Query with logical operators (since v1.2.3)

MongoDB supports queries with logical operators such as $and, $or, and $nor. See Logical Query Operators (MongoDB documentation) for more information.

To use the logical operators with LoopBack's query filter, use a where clause as follows (for example):

/server/script.js
// Find posts that have title = 'My Post' and content = 'Hello'
Post.find({where: {and: [{title: 'My Post'}, 
                         {content: 'Hello'}]}}, 
          function (err, posts) {
            ...
});
 
// Find posts that either have title = 'My Post' or content = 'Hello'
Post.find({where: {or: [{title: 'My Post'}, 
                        {content: 'Hello1'}]}}, 
          function (err, posts) {
            ...
});
 
// Find posts that neither have title = 'My Post1' nor content = 'Hello1'
Post.find({where: {nor: [{title: 'My Post1'}, 
                         {content: 'Hello1'}]}}, 
          function (err, posts) {
            ...
});