Mongoose/MongoDB - Reference another Schema without ObjectId

Viewed 3181

I currently have two schemas.

var carSchema = new mongoose.Schema({
    id: {
        type: Number,
        unique: true,
        required: true
    },
    make: {
        type: String,
        required: true
    },
    model: {
        type: String,
        required: true
    },
    year: {
        type: Number,
        required: true,
        min: config.MIN_YEAR,
        max: config.MAX_YEAR
    },
    color: {
        type: String
    },
    price: [
        // don't know what to put here...
    ],
 });


var priceSchema = new mongoose.Schema({
    car_id: {
        type: Number
    },
    amount: {
       type: Number,
       min: 0
    },
    year: {
       type: Number,
       min: config.MIN_YEAR,
       max: config.MAX_YEAR
    }
 });

As you can see from the two schemas above, I'm trying to make a reference to the price schema from the car schema. I know that you can do it by referencing the ObjectId (_id), but I need to reference the price schema through the car_id. The reason is because I'm reading all the data from a CSV file which already defined the table headers. Is there any possible way in mongoose to make this reference?

2 Answers
Related