【问题标题】:node mongoose update two collections节点猫鼬更新两个集合
【发布时间】:2020-01-20 12:58:18
【问题描述】:

我有两个架构,一个“项目”架构和一个“应用程序”架构。

在集合中创建新条目并根据新条目中的数据更新另一个集合中的现有条目的最有效方法是什么?我是否可以避免发出多个 API 请求并以某种方式在 mongoDB 端运行“存储过程”来在 Applications 集合发生更改时处理更新 Projects 集合?

在这种情况下,理想情况下,当为项目创建应用程序时,会在 Applications 集合中创建一个新条目,并更新 Projects 集合中的项目以反映应用程序中的信息。

我可以在不发出多个 api 请求的情况下做到这一点吗?

项目架构:

// models product.js

const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;

const projectSchema = new mongoose.Schema({
      name: {
        type: String,
        trim: true,
        required: true,
        maxlength: 32
      },
      applications: {
        type: Number,
        default: 0
      },
      created_by: {
        type: ObjectId,
        ref: 'User'
      },
      applicants: {
        type: Array,
        default: []
      }
       }, {timestamps: true}
);

module.exports = mongoose.model("Project", projectSchema);

应用架构:


const mongoose = require('mongoose');

const applicationSchema = new mongoose.Schema({
  applicantId: { 
    type: ObjectId,
    ref: 'User'
  },
  ownerId: { 
    type: ObjectId,
    ref: 'User'
  },
  projectId: { 
    type: ObjectId,
    ref: 'Project'
  }
}, {timestamps: true});

module.exports = mongoose.model("Application", applicationSchema);

请注意,这些是单独的架构,因为它们每个都有大约 15 个字段,我已将它们修剪下来以发布此问题。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我建议对 mongoose 模型使用钩子,有一个 post save 钩子,您可以在 Application 模型上使用它来更新项目并增加应用程序计数。

    编辑 - 添加伪代码

    1. 项目模型
    // models -> project.js
    
    const mongoose = require('mongoose');
    const {
        ObjectId
    } = mongoose.Schema;
    
    const projectSchema = new mongoose.Schema({
        name: {
            type: String,
            trim: true,
            required: true,
            maxlength: 32
        },
        applications: {
            type: Number,
            default: 0
        },
        created_by: {
            type: ObjectId,
            ref: 'User'
        },
        applicants: {
            type: Array,
            default: []
        }
    }, {
        timestamps: true
    });
    
    projectSchema.statics = {
        /**
         * Find project by _id
         *
         * @param {ObjectId} _id
         * @api private
         */
    
        get: function (_id) {
            return this.findOne({
                    _id
                })
                .exec();
        }
    }
    
    module.exports = mongoose.model("Project", projectSchema);
    
    1. 应用模型
    
    const mongoose = require('mongoose');
    const projectModel = require("./project")
    
    const applicationSchema = new mongoose.Schema({
        applicantId: {
            type: ObjectId,
            ref: 'User'
        },
        ownerId: {
            type: ObjectId,
            ref: 'User'
        },
        projectId: {
            type: ObjectId,
            ref: 'Project'
        }
    }, {
        timestamps: true
    });
    
    // Async hook
    applicationSchema.post('save', function (doc, next) {
        const relatedProject = projectModel.get(doc.projectId);
        relatedProject.applications++;
        relatedProject.save();
        next();
    });
    
    module.exports = mongoose.model("Application", applicationSchema);
    

    【讨论】:

    • 我觉得这暗示了正确的答案——你能证明一下吗?
    猜你喜欢
    • 2016-02-02
    • 1970-01-01
    • 2014-12-26
    • 2021-11-25
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    相关资源
    最近更新 更多