为此,我在同一个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, });提供了所需的解决方案! 解决方案将模式更改为此,它应该可以工作: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中,然后可以在数组中保存多个对象.I am new to web development and I just ran into a problem I can't solve. I started with the MEAN-Stack so far and I encountered the following problem.I have an array structure for a slider in my angular js Controller. From this array structure, I am trying to save a few values into my database.Angular JS Controller snippet$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' } ] } }];From the above snippet, I'd like to save the title, the value and the weight of each alert in alertsFor this purpose, I have in the same angular js controller the following snippet // Create new Article object var article = new Articles({ title: this.title, alert: this.alert, });As far as I understand does this snippet create a new article entry in my database using the following schema defined in the server.model.js filevar 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);But my Problem now is that this only saves an empty array in MongoDB.The solution presented below lead to the solution. It has to be noted thatvar 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, });Delivered the wanted solution! 解决方案 Change your schema to this and it should work: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);I managed to save arrays in mongodb by just specifying [] in the schema and then you can save multiple objects in the array. 这篇关于MEAN-Stack使用mongoose将数组保存在MongoDB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!