Setting data model as a property of a different data model

I have a data model that looks like this:

var PersonSchema = new Schema({
    id: String,
    fruit: Fruit

});


var FruitSchema = new Schema({
    type: String,
    calories: Double
});

Is it possible to set a custom object as a datatype? I'm using Express & Mongoose.

You can create custom datatypes as is referred to in the documentation, but these are generally for "datatypes" and are "plugins" such as mongoose-long that provide a new data type with expected behavior.

But you seem to be referring to referencing another "Schema" to define what is stored in a field, which is a different case. So you cannot just drop in a schema as the "type" for a field, as in fact if you tried you would get a "type error" which a message telling you that you cannot do what you are trying to do. The best way to do this is to simply inline the definition:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var personSchema = new Schema({
  name: String,
  fruit: {
    name: { type: String },
    calories: { type: Number }
  }
});

var Person = mongoose.model( "Person", personSchema );

var person = new Person({
  "name": "Bill",
  "fruit": {
    "name": "Apple",
    "calories": 52
  }
});

console.log(person);

That is allowed but it does not really help with re-use. Of course if you can live with it, then the other approach is to simply embed within an array, whether you intend to store more than one or not:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var fruitSchema = new Schema({
  name: String,
  calories: Number
});

var personSchema = new Schema({
  name: String,
  fruit: [fruitSchema]
});

var Person = mongoose.model( "Person", personSchema );

var person = new Person({
  "name": "Bill",
  "fruit": [{
    "name": "Apple",
    "calories": 52
  }]
});

console.log(person);

But really these are just JavaScript objects, so if it is just something you want to reuse in several schema definitions then all you need to do is define the object, possibly even in it's own module and then just "require" the object where you want to use it:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var Fruit = {
  name: String,
  calories: Number
};

var personSchema = new Schema({
  name: String,
  fruit: Fruit
});

var Person = mongoose.model( "Person", personSchema );

var person = new Person({
  "name": "Bill",
  "fruit": {
    "name": "Apple",
    "calories": 52
  }
});

Also noting here that "Double" in your listing is not a standard type and would indeed require a "type plugin" for mongoose-double in order to use that.