Mongoose引用嵌入式文档中的父级

时间:2015-06-17 22:54:19

标签: node.js mongodb mongoose

我对猫鼬来说相当新,所以请耐心等待一下。我定义了以下两个简单的模式。

var ShipmentSchema = new Schema({
    shipmentId: {
        type: String,
        default: '',
        required: 'Please fill Shipment Id',
        trim: true
    },
    dateInvoiced: {
        type: Date,
        default: Date.now
    },
    vendorInvoices:
    [
        {
            referenceNo: {type: Schema.ObjectId,ref: 'VenderInvoice'}
        }
    ],
});

mongoose.model('Shipment', ShipmentSchema);

var VendorInvoiceSchema = new Schema({
    type: {
        type: String,
        default: '',
        trim: true
    },
    referenceNo: {
        type: String,
        default: '',
        trim: true
    },
    cases: {
        type: Number,
        required: 'Please fill in number of cases',
    },
    dateShipped: {
        type: Date,
        default: Date.now
    },
    invoicedProducts:
    [
        {
            productId: {type: Schema.ObjectId,ref: 'Shoe'},
        }
    ]
});

mongoose.model('VenderInvoice', VendorInvoiceSchema);

如果我想获得VendorInvoices列表并希望包含他们所属的货件,有没有办法这样做?我知道如何获得货物及其嵌套的发票清单,但如果我想转向另一个方向有办法吗?或者在这种情况下我需要规范化吗?

谢谢

0 个答案:

没有答案