mongoose

Mongoose Schemas

Creating a Schema

var mongoose = require('mongoose');

//assume Player and Board schemas are already made
var Player = mongoose.model('Player'); 
var Board = mongoose.model('Board');

//Each key in the schema is associated with schema type (ie. String, Number, Date, etc)
var gameSchema = new mongoose.Schema({
  name: String,
  players: [{
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'Player'
  }],
  host: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Player'
  },
  board: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Board'
  },
  active: {
    type: Boolean,
    default: true
  },
  state: {
    type: String,
    enum: ['decision', 'run', 'waiting'],
    default: 'waiting'
  },
  numFlags: {
    type: Number,
    enum: [1,2,3,4]
  },
  isWon: {
    type: Boolean,
    default: false
  }
});

mongoose.model('Game', gameSchema);

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow