Installation
In your application root directory, enter:
$ 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:
"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
Property | Type | Description |
---|---|---|
connector | String | Connector name, either "loopback-connector-mongodb" or "mongodb" |
database | String | Database name |
debug | Boolean | If true, turn on verbose mode to debug database queries and lifecycle. |
host | String | Database host name or IP address. |
password | String | Password to connect to database, if required. |
port | Number | Database TCP port |
url | String | Connection string URI; see http://docs.mongodb.org/manual/reference/connection-string/. See Replica set configuration below. |
username | String | Username to connect to database, if required. |
For example:
{ "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
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.
{ "name": "Post", "mongodb": { "collection": "PostCollection" }, "properties": { ... } }
Replica set configuration
{ “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:
var ds = app.dataSources.db; MyModel = ds.createModel('mymodel', { _id: { type: ds.ObjectID, id: true } });
Example with a Number _id :
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):
// 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) { ... });