簡體   English   中英

MEAN-Stack用mongoose將數組保存在MongoDB中

[英]MEAN-Stack save an array in MongoDB with mongoose

我是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中,然后可以在數組中保存多個對象。

您需要在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

首先,您需要創建一條路由,該路由由您的有角$ 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);
    }
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM