0
我有一个股票应用程序,我想设置有关股票的一些细节,然后插入股票的所有项目。我想在两个不同的集合中插入库存细节和项目,然后我可以过滤这些项目。我使用的平均堆栈,我已经修改了污物模块接受一些额外的领域,也取得了填充的项目的UI array.This是我到目前为止有:同时保存两个参考文档
scope.stockItems = [];
$scope.createStockItem = function() {
$scope.stockItems.push(
{
brand: $scope.brand,
style: $scope.style,
amount: $scope.amount
}
);
$scope.brand = false;
$scope.style = false;
$scope.amount = '';
};
// Create new Stock
$scope.create = function() {
// Create new Stock object
var stock = new Stocks ({
name: this.name,
details: this.details,
stockDate: this.stockDate
});
// Redirect after save
stock.$save(function(response) {
$location.path('stocks/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
股票模型:
var StockSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Stock name',
trim: true
},
details: {
type: String,
default: '',
required: 'Please fill Stock details'
},
stockDate: Date
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
,并在服务器控制器的方法:
exports.create = function(req, res) {
var stock = new Stock(req.body);
stock.user = req.user;
stock.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(stock);
}
});
};
如何发送到请求并保存stockItems也?
所以你我可以使用非规范化的数据模型,并在stocks中嵌入stockItems。但是使用这个模型可以查询所有股票中的所有红股票吗? – 2014-09-19 09:49:35
@FacuFerrari是的我的意思是你可以将库存物品存储在嵌入式文档中,这样当你保存它们时它就是原子。我真的不知道你将如何去查询'红股'。你是否有另外一批物品库存,每个记录代表一个特定物品的库存? – yaoxing 2014-09-21 07:15:16