Skip to content

mongoose

Sébastien LUCAS edited this page Aug 12, 2016 · 12 revisions

Plugins

Mongoose plugins

Relation

Population

http://mongoosejs.com/docs/populate.html

var league; The author speak of a method to save a new element even with population

league = {
  display_name: myLeagueName,
  created_by: currentPlayer._id
};

League.create(league, function(err, league) {
  if (err != null) {

  } else {
    return Player.update({
      _id: currentPlayer._id
    }, {
      $push: {
        leagues: league._id
      }
    }, function(err, numberAffected, raw) {
      if (err != null) {

      } else {
        return res.json(league);
      }
    });
  }
});

Virtual field

Pseudo field to return information that are not stored in mongodb (for example the concatenation of first name + last name)

https://futurestud.io/blog/understanding-virtuals-in-mongoose

Best pratices

WIKI by Sébastien Lucas CEO & Funder or Bricks

Clone this wiki locally