我正在使用2个数据库的NodeJS项目
现在,我想执行一个使用填充的查询,并且在我的数据库中,我没有使用任何“ ref”关系
有什么方法可以使用populate方法执行查询而无需更改数据库架构?
这是我的关联架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var AssociateSchema = new Schema({
category_id:{type: ObjectId, max: 100},
associate_name: {type:String,max: 100},
associate_address: {type:String,max: 500},
associate_no: {type:Number},
associate_emaiid: {type:String,max: 100},
associate_status: {type:Boolean},
});
Associate= mongoose.model('Associate', AssociateSchema);
module.exports = mongoose.model('Associate', AssociateSchema);
这是我的出口模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var OutletSchema = new Schema({
associate_id:{type:ObjectId, max: 100},
outlet_name: {type:String,max: 100},
outlet_address: {type:String,max: 500},
outlet_no: {type:Number},
outlet_status: {type:Boolean},
city_id:{type:ObjectId, max: 100},
});
module.exports = mongoose.model('Outlet', OutletSchema);
这是我正在使用的查询:
router.get('/getma', function (req, res) {
Associate.findOne({ _id: '' })
.populate('outlets')
.exec(function (err,dea) {
if (err) return handleError(err);
console.log(dea)
res.json(dea)
})
});
感谢:)