【问题标题】:Push a sub-subdocument on a Mongoose Schema [duplicate]在 Mongoose Schema 上推送子子文档 [重复]
【发布时间】:2020-03-02 06:40:26
【问题描述】:

考虑这 3 个架构和层次结构:一个项目有多个阶段,一个阶段有多个事件。 为了将新阶段推入项目,我这样做:

Project.findOneAndUpdate(
      { slug: projectSlug },
      { $push: { stages: myNewStage } },
    ).then((post) => res.status(201).json({
      message: 'stage created successfully',
      data: post,
    })).catch((error) => {
      return res.status(500).json({
        code: 'SERVER_ERROR',
        description: 'something went wrong, Please try again',
      });
    });

但是,如何将新事件推送到舞台中?据我所知,子文档与文档的属性不同(例如 find、findAndUpdate)。

我的实际架构:

项目架构

const mongoose = require('mongoose');
const Stage = require('./Stages').model('Stages').schema;

const projectSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
  },
  slug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
    unique: true,
  },
  clientSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  stages: [Stage],
  startDate: {
    type: Date,
    trim: true,
    required: true,
  },
  endDate: {
    type: Date,
    trim: true,
    required: false,
  },
},
  {
    timestamps: true,
  });

module.exports = mongoose.model('Projects', projectSchema);

舞台架构

const mongoose = require('mongoose');
const Event = require('./Events').model('Events').schema;

const stageSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
  },
  slug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
    unique: true,
  },
  clientSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  projectSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  events: [Event],
},
  {
    timestamps: true,
  });

module.exports = mongoose.model('Stages', stageSchema);

事件模式

const mongoose = require('mongoose');
const Comment = require('./Comments').model('Comments').schema;

const eventSchema = new mongoose.Schema({
  _id: {
    type: String,
    trim: true,
    lowercase: true,
  },
  userEmail: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  text: {
    type: String,
  },
  imgUrl: {
    type: String,
  },
  documentUrl: {
    type: String,
  },
  stageSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  clientSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  projectSlug: {
    type: String,
    trim: true,
    required: true,
    lowercase: true,
  },
  comments: [Comment],
},
  {
    timestamps: true,
  });

module.exports = mongoose.model('Events', eventSchema);

【问题讨论】:

  • 您不应该将活动推入阶段吗?
  • @OTZ 是的,对不起。我刚刚更新了我的问题。
  • 拜托,您能分享一下您的Project 模型的架构或示例项目文档吗?
  • 完成。谢谢!

标签: mongodb mongoose


【解决方案1】:

鉴于您拥有projectSlugstageSlug,要将新事件推送到您的阶段数组中,您可以这样做:

Project.findOneAndUpdate(
  {
    $and: [
      { slug: projectSlug },
      { 'stages.slug': stageSlug },
    ]
  },
  {
    $push: { 'stages.$.events': newEvent }
  }
)
.then(() => {})
.catch(() => {});

【讨论】:

  • 谢谢你,它就像一个魅力。只有一个问题,我一次可以使用超过一个 $('stages.$.events')吗?例如,将评论添加到事件中。
  • 据我所知,你只能使用一次(深一层)。但是,如果您需要更新您描述的嵌套数组,您可以使用arrayFilters update option,您可以指定多个位置匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2021-05-06
  • 1970-01-01
  • 2018-02-02
相关资源
最近更新 更多