nested schema mongoose code example

Example 1: nested json schema mongoose

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

var msg = new Schema({
  messageType: String,
  timestamp: Number,
  messagestatus: String
});

var standardmessage = new Schema({
  id: Number,
  name: String,
  type: String,
  message: [msg]
});

Example 2: mongoose select nested

var fields = { 'properties.OBJECTID': 1, 'properties.TIMESTAMP': 1 };
var query = Feature.find({id: 1}).select(fields);

Example 3: mongoose nested require

var jobSchema = Schema({
  negotiation: { state: { type: String, required: hasNegotiation } }
});

function hasNegotiation() {
  return this.negotiation && Object.keys(this.negotiation).length > 0;
}

Example 4: nested json schema mongoose

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

var standardmessage = new Schema({
  id: Number,
  name: String,
  type: String,
  message: {
    messageType: String,
    timestamp: Number,
    messagestatus: String
  }
});

Tags: