With the AddressSchema
defined, you can now define the billing schema to keep track of credit card information as well as billing information. Listing 28.2 implements BillingSchema
, which contains standard credit card data. Notice that the following implementation for cardtype
requires entry of Visa
, MasterCard
, or Amex
(not any other values):
cardtype: { type: String, enum: ['Visa', 'MasterCard', 'Amex'] },
Also notice that the address
field is assigned the AddressSchema
object type. Mongoose requires you to include nesting schemas in an array. You’ll see this in several places in this example, and you’ll see that throughout the example, that the address is accessed by using address[0]
to get the first item in the array.
11 var BillingSchema = new Schema({
12 cardtype: { type: String, enum: ['Visa', 'MasterCard', 'Amex'] },
13 name: String,
14 number: String,
15 expiremonth: Number,
16 expireyear: Number,
17 address: [AddressSchema]
18 }, { _id: false });
19 mongoose.model('Billing', BillingSchema);