猫鼬 |对所有函数保持相同的 Population
Mongoose | Keep the same Population to all functions
I Have this schema for my Crop Model
var CropSchema = new mongoose.Schema({
name: String,
zones: [{
type: Schema.Types.ObjectId,
ref: 'zone'
}],
...
});
This schema for my Zone Model
var ZoneSchema = new mongoose.Schema({
name: String,
poor: [{
type: Schema.Types.ObjectId,
ref: 'condition'
}],
...
});
This schema for my Condition Model
var ConditionSchema = new mongoose.Schema({
name: String,
action_on_controls: [{
type: Schema.Types.ObjectId,
ref: 'control'
}],
...
});
And this schema for my Control Model
var ControlSchema = new mongoose.Schema({
name: String,
...
});
The way I am getting all the Crops in Node is this:
public index(req: Request, res: Response) {
return Crop.find().populate('zones').populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
The way I am getting an individual Crop is this:
public show(req: Request, res: Response) {
return Crop.findById(req.params.id).populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
如你所见,部分:
.populate({..})
重复两次。
如何保持相同的填充配置,这样我就不必一直write/update做同样的事情?
您可以将填充对象保存为变量并共享它:
const zonePopulateObj = {
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
};
然后在您的查询中
return Crop.find().populate(zonePopulateObj).exec();
return Crop.findById(req.params.id).populate(zonePopulateObj).exec();
或者您可以将查询逻辑拉入一个新函数并共享它
public index(req: Request, res: Response) {
return findCrop()
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
public show(req: Request, res: Response) {
return findCrop(req.params.id)
.then((array)=>array.length ? array[0] : {})
.then(handleEntityNotFound(res)) // may need to update this function not sure how it checks for not found.
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
const findCrop = (id)=>{
let queryObj = {};
if(id){
queryObj._id=id
}
return Crop.find(queryObj).populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
}
个人比较喜欢第一种方式。
I Have this schema for my Crop Model
var CropSchema = new mongoose.Schema({
name: String,
zones: [{
type: Schema.Types.ObjectId,
ref: 'zone'
}],
...
});
This schema for my Zone Model
var ZoneSchema = new mongoose.Schema({
name: String,
poor: [{
type: Schema.Types.ObjectId,
ref: 'condition'
}],
...
});
This schema for my Condition Model
var ConditionSchema = new mongoose.Schema({
name: String,
action_on_controls: [{
type: Schema.Types.ObjectId,
ref: 'control'
}],
...
});
And this schema for my Control Model
var ControlSchema = new mongoose.Schema({
name: String,
...
});
The way I am getting all the Crops in Node is this:
public index(req: Request, res: Response) {
return Crop.find().populate('zones').populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
The way I am getting an individual Crop is this:
public show(req: Request, res: Response) {
return Crop.findById(req.params.id).populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
如你所见,部分:
.populate({..})
重复两次。
如何保持相同的填充配置,这样我就不必一直write/update做同样的事情?
您可以将填充对象保存为变量并共享它:
const zonePopulateObj = {
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
};
然后在您的查询中
return Crop.find().populate(zonePopulateObj).exec();
return Crop.findById(req.params.id).populate(zonePopulateObj).exec();
或者您可以将查询逻辑拉入一个新函数并共享它
public index(req: Request, res: Response) {
return findCrop()
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
public show(req: Request, res: Response) {
return findCrop(req.params.id)
.then((array)=>array.length ? array[0] : {})
.then(handleEntityNotFound(res)) // may need to update this function not sure how it checks for not found.
.then(respondWithResult(res, 200))
.catch(handleError(res, 500));
}
const findCrop = (id)=>{
let queryObj = {};
if(id){
queryObj._id=id
}
return Crop.find(queryObj).populate({
path: 'zones',
populate: [
{
path: 'poor', populate: [
{ path: 'action_on_controls' }]
}
]
}).exec()
}
个人比较喜欢第一种方式。