【问题标题】:Getting nested array references using mongoose schema?使用猫鼬模式获取嵌套数组引用?
【发布时间】:2014-08-13 02:57:03
【问题描述】:

我有下面给出的格式的架构。

var parentSchema = mongoose.Schema({
  id: Schema.Types.ObjectId,
  username: { type: String, required: true, index: { unique: true } },
  password: { type: String, required: true },
  country_id: Schema.Types.ObjectId,
  children: [
    {
      id: Schema.Types.ObjectId,
      child_name: String,
      standard_id: Schema.Types.ObjectId,
      total_gold: { type: Number },
      total_diamonds: { type: Number },
      total_hearts: { type: Number },
      subjects: [
        {
          subject_name: String,
          grade_name: String,
          games: [{ id: String, name: String }],
        },
      ],
    },
  ],
});

module.exports = mongoose.model("Parent", parentSchema);

Parent 是模型,children 是一个对象数组,内部包含一个名为subjects 的数组。主题还包含一个名为 games 的数组。

如何使用模型将数据推送到主题和游戏数组中? 这就是我正在做的。

Parent.findOne({ username: req.body.username }, function (err, parent) {
  if (err) res.json(err);
  // if the user is found, then log them in
  if (parent) {
    res.json(parent);
  } else {
    var parent = new Parent();
    parent.username = req.body.username;
    parent.password = req.body.password;
    parent.children.push({
      child_name: req.body.childname,
      total_gold: 0,
      total_diamonds: 0,
      total_hearts: 0,
    });
  }
});

如何将数据推送到主题数组和游戏数组中?

【问题讨论】:

  • 不清楚您所说的“我如何访问”是什么意思。您需要在问题中解释的是您想要做什么。更新?获得某些物品?
  • 我要向数组中添加数据?
  • 哪个数组?有几种。如何在您的问题中添加一些代码,甚至是失败的代码,以显示您正在尝试做的事情。
  • 您的编辑现在实际上使您的架构无效。正如我之前所说,您最好的方法是显示一些“损坏的代码”来解释您正在尝试做什么。请不要要求完整的 CRUD 教程。有很多示例供您搜索。
  • 我建议你回顾一下你为嵌套模式编写代码的方式,并做一些更类似于:Mongoose/MongoDB 中嵌套模式的正确模式是什么?

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

要回答您的问题,您应该制作不同的架构并添加来自不同模型的对象。在父模式中,您只需将 objectids 保存为对子节点的引用,如下所示:

var childSchema = mongoose.Schema({
    child_name: String,
    standard_id: mongoose.Schema.Types.ObjectId,
    total_gold: { type: Number},
    total_diamonds:{ type: Number },
    total_hearts: { type: Number},
    subjects: [
        {
            subject_name :String,
            grade_name : String,
            games: [ 
                { 
                    id: String,
                    name: String
                }
            ]
        }
    ]
});
mongoose.model('Child', childSchema);

var parentSchema = mongoose.Schema({
    username: { type: String, required: true, index: { unique: true } },
    password: { type: String, required: true },
    country_id : mongoose.Schema.Types.ObjectId,
    children: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Child'
        }
    ]
});
mongoose.model('Parent', parentSchema);

稍后您可以进行填充,以获取有关此检查的更多信息http://mongoosejs.com/docs/populate.html

根据请求,示例正文将是:

{
    child_name: 'name',
    total_gold: 23,
    total_diamonds: 14,
    total_hearts: 21,
    subjects: [
        {
            subject_name: 'this is an example name',
            grade_name: 'this is an example grade name',
            games: [
                {
                    id: 'id1',
                    name: 'name1'
                },
                {
                    id: 'id2',
                    name: 'name2'
                }
            ]
        }
    ]
}

添加新项目:

Child.create(req.body, function (err, doc) {
  if (err) {
      // Do something with the error
  }
  else {
      // send the response
  }
});

【讨论】:

  • 这真的应该是一个评论,除非你想修饰并真正回答这个问题。这并不是说您缺少代表分数来添加评论。
  • 我该怎么做。孩子和父母会成为单独的集合还是将子模式添加到父母的 json 对象中?
  • 如何将数据推送到游戏数组中?我还需要为游戏数组定义单独的架构吗?
  • 它们是嵌入在同一个对象中的对象数组。
  • 在上面给定的架构中,您在主题数组中定义了游戏数组。当我为主题数组创建模型时,我如何访问游戏数组。例如:我可以访问主题.主题名;我可以这样访问 subject.games.push() 吗?
猜你喜欢
  • 2019-03-22
  • 2018-10-13
  • 2019-02-24
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 2018-10-30
  • 2021-10-06
相关资源
最近更新 更多