MEAN-Stack 用 mongoose 在 MongoDB 中保存一个数组
MEAN-Stack save an array in MongoDB with mongoose
我是网络开发的新手,我只是 运行 遇到了一个我无法解决的问题。到目前为止,我从 MEAN-Stack 开始,但遇到了以下问题。
我的 angular js 控制器中有一个滑块数组结构。从这个数组结构中,我试图将一些值保存到我的数据库中。
Angular JS 控制器片段
$scope.alerts = [
{ id: 1,
title: 'CustomerCount',
value: 1,
weight: 30,
options: {
showTicks: true,
hidePointerLabels: true,
hideLimitLabels: true,
stepsArray: [
{ value: 1, legend: '<10' },
{ value: 2, legend: '<50' },
{ value: 3, legend: '<250' },
{ value: 4, legend: '<500' },
{ value: 5, legend: '>500' }
]
}
},
{ id: 2,
title: 'CustomerSatisfaction',
value: 3,
weight: 40,
options: {
showTicks: true,
hidePointerLabels: true,
hideLimitLabels: true,
stepsArray: [
{ value: 1, legend: '<10' },
{ value: 2, legend: '<50' },
{ value: 3, legend: '<250' },
{ value: 4, legend: '<500' },
{ value: 5, legend: '>500' }
]
}
}
];
从上面的片段中,我想保存标题、值和权重 警报中的每个警报
为此,我在同一个 angular js 控制器中有以下代码片段
// Create new Article object
var article = new Articles({
title: this.title,
alert: this.alert,
});
据我了解,此代码段是否使用 server.model.js 文件
中定义的以下模式在我的数据库中创建了一个新文章条目
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alert: [{ value: Number, weight: Number, title: String }]
});
mongoose.model('Article', ArticleSchema);
但我现在的问题是,这只会在 MongoDB 中保存一个空数组。
下面介绍的解决方案引出了解决方案。需要注意的是
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alerts: []
});
// Create new Article object
var article = new Articles({
title: this.title,
alerts: this.alerts,
});
提供了想要的解决方案!
您需要在 Article
架构上调用 save
方法。才保存文章
var article = new Articles({
title: this.title,
alert: this.alert,
});
article.save(function(err, doc){
if(!err){
// Article saved! "doc" is the reference
}else{
console.error('Error on saving the article');
}
});
如果您使用的是 MEAN,则保存数据来自服务器,该服务器处理 Express、Mongodb(Mongoose) 和 Nodejs。
我假设您使用的是 yo
的 Meanjs
首先你需要创建一个路由,你的 angular $http ajax
路线:
article.server.route.js
// Articles collection routes
app.route('/api/articles')
.post(articles.create);
控制器:
article.server.controller.js
exports.create = function (req, res) {
var article = new Article(req.body);
article.user = req.user;
article.save(function (err) {
if (err) {
return res.status(400).send({
message: err
});
} else {
res.json(article);
}
});
};
将您的架构更改为此,它应该可以工作:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alert: []
});
mongoose.model('Article', ArticleSchema);
我通过在架构中指定 []
设法将数组保存在 mongodb 中,然后您可以在数组中保存多个对象。
我是网络开发的新手,我只是 运行 遇到了一个我无法解决的问题。到目前为止,我从 MEAN-Stack 开始,但遇到了以下问题。
我的 angular js 控制器中有一个滑块数组结构。从这个数组结构中,我试图将一些值保存到我的数据库中。
Angular JS 控制器片段
$scope.alerts = [
{ id: 1,
title: 'CustomerCount',
value: 1,
weight: 30,
options: {
showTicks: true,
hidePointerLabels: true,
hideLimitLabels: true,
stepsArray: [
{ value: 1, legend: '<10' },
{ value: 2, legend: '<50' },
{ value: 3, legend: '<250' },
{ value: 4, legend: '<500' },
{ value: 5, legend: '>500' }
]
}
},
{ id: 2,
title: 'CustomerSatisfaction',
value: 3,
weight: 40,
options: {
showTicks: true,
hidePointerLabels: true,
hideLimitLabels: true,
stepsArray: [
{ value: 1, legend: '<10' },
{ value: 2, legend: '<50' },
{ value: 3, legend: '<250' },
{ value: 4, legend: '<500' },
{ value: 5, legend: '>500' }
]
}
}
];
从上面的片段中,我想保存标题、值和权重 警报中的每个警报
为此,我在同一个 angular js 控制器中有以下代码片段
// Create new Article object
var article = new Articles({
title: this.title,
alert: this.alert,
});
据我了解,此代码段是否使用 server.model.js 文件
中定义的以下模式在我的数据库中创建了一个新文章条目var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alert: [{ value: Number, weight: Number, title: String }]
});
mongoose.model('Article', ArticleSchema);
但我现在的问题是,这只会在 MongoDB 中保存一个空数组。
下面介绍的解决方案引出了解决方案。需要注意的是
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alerts: []
});
// Create new Article object
var article = new Articles({
title: this.title,
alerts: this.alerts,
});
提供了想要的解决方案!
您需要在 Article
架构上调用 save
方法。才保存文章
var article = new Articles({
title: this.title,
alert: this.alert,
});
article.save(function(err, doc){
if(!err){
// Article saved! "doc" is the reference
}else{
console.error('Error on saving the article');
}
});
如果您使用的是 MEAN,则保存数据来自服务器,该服务器处理 Express、Mongodb(Mongoose) 和 Nodejs。
我假设您使用的是 yo
的 Meanjs首先你需要创建一个路由,你的 angular $http ajax 路线: article.server.route.js
// Articles collection routes
app.route('/api/articles')
.post(articles.create);
控制器: article.server.controller.js
exports.create = function (req, res) {
var article = new Article(req.body);
article.user = req.user;
article.save(function (err) {
if (err) {
return res.status(400).send({
message: err
});
} else {
res.json(article);
}
});
};
将您的架构更改为此,它应该可以工作:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
alert: []
});
mongoose.model('Article', ArticleSchema);
我通过在架构中指定 []
设法将数组保存在 mongodb 中,然后您可以在数组中保存多个对象。