I\'ve seen many examples about mongoose and relations, but how can I create a reference to another entity into a custom field ?
var mongoose = require(\'mongoose
No, you can't. Mongoose always use _id field to link documents. But...
You can set your own _id for each document, using any datatype you want. There are only two restrictions:
So, instead of adding new slugname field, use author's _id as a slugname:
var Author = m.model('Author', new m.Schema({
_id: String, // <-- slugname
name: String
}));
var Book = m.model('Book', new m.Schema({
title: String,
author: { type: String, ref: 'Author' }
}));