More information can be found here: http://mongoosejs.com/docs/guide.html
npm install --save mongodb
npm install --save mongoose //A simple wrapper for ease of development
In your server file (normally named index.js or server.js)
const express = require('express');
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const mongoConnectString = 'http://localhost/database name';
mongoose.connect(mongoConnectString, (err) => {
if (err) {
console.log('Could not connect to the database');
}
});
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const Article = new Schema({
title: {
type: String,
unique: true,
required: [true, 'Article must have title']
},
author: {
type: ObjectId,
ref: 'User'
}
});
module.exports = mongoose.model('Article, Article);
Let's dissect this. MongoDB and Mongoose use JSON(actually BSON, but that's irrelevant here) as the data format. At the top, I've set a few variables to reduce typing.
I create a new Schema
and assign it to a constant. It's simple JSON, and each attribute is another Object with properties that help enforce a more consistent schema. Unique forces new instances being inserted in the database to, obviously, be unique. This is great for preventing a user creating multiple accounts on a service.
Required is another, declared as an array. The first element is the boolean value, and the second the error message should the value being inserted or updated fail to exist.
ObjectIds are used for relationships between Models. Examples might be 'Users have many Comments`. Other attributes can be used instead of ObjectId. Strings like a username is one example.
Lastly, exporting the model for use with your API routes provides access to your schema.
A simple GET request. Let's assume the Model from the example above is in the file ./db/models/Article.js
.
const express = require('express');
const Articles = require('./db/models/Article');
module.exports = function (app) {
const routes = express.Router();
routes.get('/articles', (req, res) => {
Articles.find().limit(5).lean().exec((err, doc) => {
if (doc.length > 0) {
res.send({ data: doc });
} else {
res.send({ success: false, message: 'No documents retrieved' });
}
});
});
app.use('/api', routes);
};
We can now get the data from our database by sending an HTTP request to this endpoint. A few key things, though:
find
instead of findOne
, confirm that the doc.length
is greater than 0. This is because find
always returns an array, so an empty array will not handle your error unless it is checked for lengthconst app = express();
require('./path/to/this/file')(app) //