簡體   English   中英

將JSON數組添加到Mongoose模式(JavaScript)

[英]Adding JSON array to a Mongoose schema (JavaScript)

我正在創建一個Android應用程序(棒球應用程序),並在其中使用MongoDB存儲數據。 我希望將JSON數據存儲到數據庫中的方式是這樣的。

{"email@domain.com":{
      "coachName": "Smith"
       players:[
            player1:{
                "throws_":"n\/a",
                "position":"position not set",
                "number":"1",
                "playerNum":"H8E83NxRo6",
                "bats":"n\/a",
                "team_name":"Team",
                "name":"Name"}
             player2:{
                "throws_":"n\/a",
                "position":"position not set",
                "number":"1",
                "playerNum":"H8E83NxRo6",
                "bats":"n\/a",
                "team_name":"Team",
                "name":"Name"}
      ]  
}

抱歉,如果有任何語法錯誤,但實際上這是我想要JSON的布局。 mongo頁面“ id”是人員電子郵件。 而“球員”是教練擁有的球員列表的數組。 我的問題是,我怎么能

  1. 正確設置貓鼬模式看起來像這樣嗎?

  2. 當應用程序發送JSON數據時,如何解析它以存儲數據?

  3. 並且如果可能的話(如果沒有人的話,請嘗試嘗試自己摸索這一部分),如果一次添加多個播放器,如果陣列中已經有播放器,我如何存儲它們?

所有這些都是后端/服務器端,我讓android正常工作,我只需要幫助將其存儲為JavaScript即可。

  1. 您不想使用動態變量作為字段名稱。 我說的是您的電子郵件地址為“ email@domain.com”。 那樣不好,因為您將如何找到對象。 通過那里的字段搜索對象並不常見,您可以使用字段名稱來描述您要查找的內容。 您將需要2個模型。 一個給球員,一個給教練。 教練在其玩家數組字段中對玩家進行了刷新。
  2. 如果正確格式化JSON,則不必進行任何解析,只需確保要發送的JSON的格式正確即可。
  3. 查看addPlayer函數。

控制器文件 (問題2和3的答案)

create = function(req, res) {
    var coach = new Coach(req.body);
    coach.user = req.user;

    coach.save(function(err) {
        if (err) {
            return res.status(400).send({
                // Put your error message here
            });
        } else {
            res.json(coach);
        }
    });
};

read = function(req, res) {
    res.json(req.coach);


};

addPlayer = function(req, res) {
    var coach = req.coach;
    console.log('updating coach', req.coach);
    var player = new Player(req.newPlayer);
    coach.players.push(newPlayer);

    coach.save(function(err) {
        if (err) {
            return res.status(400).send({
                // Put your error message here
            });
        } else {
            res.json(coach);
        }
    });
};



播放器

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

/**
 * Player Schema
 */
var PlayerSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    throws: {
        type: Number,
    },
    name: {
        type: String,
        default: '',
        trim: true
    },
    position: {
        type: String,
        default: '',
        trim: true
    },
    playerNum: {
        type: String,
        default: '',
        trim: true
    },
    position: {
        type: Number,
        default: 0,
        trim: true
    },
    teamName: {
        type: String,
        default: '',
        trim: true
    }
});

mongoose.model('Player', PlayerSchema);

教練模式

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

/**
 * Coach Schema
 */
var CoachSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  email: {
    type: String,
    unique: true,
    lowercase: true,
    trim: true,
    default: ''
  },
  name: {
    type: String,
    default: '',
    trim: true
  },
  players: [{
    type: Schema.ObjectId,
    ref: 'Player'
  }
});

mongoose.model('Coach', CoachSchema);

暫無
暫無

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

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