Mongoose - 查询第三层子文档

时间:2016-05-04 13:15:22

标签: node.js mongodb mongoose

是否可以在两个嵌套层下查询文档?我有时间价格是供应商的子文件,供应商是材料的子文件。我试过这个并不行:

//schema
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//price schema for all
var priceSchema = new Schema({
    price:  {
        type: Number
    }
}, {
    timestamps: true
});

//supplier and market schema
var supplierSchema = new Schema({
    //1
    name:  {
        type: String
    },
    //2
    priceAtDepot: [priceSchema],
    //3
    depotLocation:  {
        type: String
    },
    //4
    destination:  {
        type: String
    },
    //5
    distance:  {
        type: Number
    },
    //6
    minUnit:  {
        type: Number
    },
    //7
    shippingFee:  {
        type: Number
    },
    //8
    shippingFeePitemPkm:  {
        type: Number
    },
    //9
    contactPerson:  {
        type: String
    },
    //10
    phone:  {
        type: String
    },
    //11
    email:  {
        type: String
    }
}, {
    timestamps: true
});





//material schema
var materialSchema = new Schema({
    //1
    materialType: {
        type: String,
        required: true
    },
    //2
    producer: {
        type: String,
        required: true
    },
    //3
    materialName: {
        type: String,
        required: true
    },
    //4
    supplier: [supplierSchema],
    //5
    market: [supplierSchema],
    //6
    namedSupplier: {
        type: String
    },
    //7
    namedMarketPirce: {
        type: Number
    },
    //8
    marginAbs: {
        type: Number
    },
    //9
    marginPercentage: {
        type: Number
    },
    //10
    namedWebPrice: {
        type: [priceSchema]
    },
    //11
    saving: {
        type: Number
    },
    //12
    savingPercentage: {
        type: Number
    },
    //13
    minUnit: {
        type: Number
    },
    //14
    shippingFeePitemPkm: {
        type: Number
    },
    //15
    techData1: {
        type: String
    },
    //16
    techData2: {
        type: String
    },
    //17
    techData3: {
        type: String
    },
    //18
    techData4: {
        type: String
    },
    //19
    techData5: {
        type: String
    },
    //20
    techData6: {
        type: String
    },
    //21
    techData7: {
        type: String
    },
    //22
    techData8: {
        type: String
    },
    //23
    techData9: {
        type: String
    },
    //24
    techData10: {
        type: String
    }
}, {
    timestamps: true
});

// the schema is useless so far
// we need to create a model using it
var Material = mongoose.model('Material', materialSchema);
// make this available to our Node applications
module.exports = Material;


//router
materialRouter.route('/:materialId/supplier/:supplierId/price')
    .get(function (req, res, next) {
        Material.findById(req.params.materialId, function (err, material) {
            if (err) throw err;         
            console.log('Get all prices from a supplier '+ req.params.materialId +' and ' + req.params.supplierId);
            material.supplier.findById(req.params.supplierId, function (err, Suppliers) {
                if (err) throw err;
                    res.json(Suppliers.priceAtDepot);
            });
        });
    })

如果无法使用此方法,是否有人可以建议其他方法来实现?如果我必须将子文档“supplier”放到文档中,将会重复大量信息。

0 个答案:

没有答案