MEAN-Stack使用mongoose将数组保存在MongoDB中 [英] MEAN-Stack save an array in MongoDB with mongoose

查看:96
本文介绍了MEAN-Stack使用mongoose将数组保存在MongoDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web开发的新手,但遇到一个我无法解决的问题.到目前为止,我是从 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' }

        ]
      }
    }
];

在上面的代码段中,我想保存警报中每个警报的标题权重

为此,我在同一个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 alerts

For 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 file

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);

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 that

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,
  });

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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆