如何使用FeatherJS处理多对少mongodb关系?
我有两个模型,具有多对少关系,我正在建模如下:如何使用FeatherJS处理多对少mongodb关系?,mongodb,mongoose,feathersjs,feathers-service,Mongodb,Mongoose,Feathersjs,Feathers Service,我有两个模型,具有多对少关系,我正在建模如下: // Portfolio const portfoliosSchema = new Schema({ name: { type: String, required: true }, description: { type: String }, user: { type: Schema.Types.ObjectId, ref: 'User', required: true }, positions: [{ stock: { t
// Portfolio
const portfoliosSchema = new Schema({
name: { type: String, required: true },
description: { type: String },
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
positions: [{
stock: { type: Schema.Types.ObjectId, ref: 'Stock', required: true },
cost: number,
amount: number,
}]
});
// Stock
const stocksSchema = new Schema({
exchange: { type: String, required: true },
symbol: { type: String, required: true },
company: { type: String },
description: { type: String }
});
如果不以友好的方式编写自定义服务,我将如何:
function(hook) {
const query = hook.params.query;
hook.params.query = Object.assign({},query,{
$populate: {
path: 'positions.stock',
select: 'exchange symbol'
}
});
}
填充代码示例,根据您的需要进行调整:
Portfolio.find({ some: 'params' })
.populate({ path: 'stock', select: 'name -_id' })
.exec((err, portfolios) => {
res.json({ portfolio: portfolios });
});
正在更新嵌套数组项:
Position.update({ 'position._id': 'h43q9h94945d948jh098j6h0' }, {
'$set': { 'position.$.whatever': 'your-updated-stuff' }
}, err => {
if (err) return console.log(err));
res.json({ success: true });
});
是的,这两个都受支持,正在为您寻找一些示例代码。嗨,艾伦,如果我错了请纠正我,但这是如何创建这些查询的mongoose代码。我将如何在feathersjs中使用它-我必须使用这些查询创建自定义服务吗?