如何以猫鼬添加到多个模型?

时间:2019-07-10 15:36:21

标签: express mongoose graphql

我想使用一个有效负载将其更新/推送到多个猫鼬模式。例如。我有一个VolSchema,一个CaseSchema和一个JudgeSchema。每当创建新的VolSchema时,我都希望将值推入JudgeSchemaCase。当我仅更新VolSchema时,它已经可以正常工作,但是当添加JudgeSchema时,我只更新了法官模型,而没有Vol模型。

Model / Cases.js

const mongoose = require("mongoose");
const CasesSchema = new mongoose.Schema({
title: String,
vol: { type: mongoose.Schema.Types.ObjectId, ref: "Vol", required: 
true },
judges: [{ type: mongoose.Schema.Types.ObjectId, ref: "Judge" }],
});

module.exports = mongoose.model("Cases", CasesSchema)

模型/Vol.js

const mongoose = require("mongoose");
const VolSchema = new mongoose.Schema({
vol_no: {
    type: String,
    unique: true,
    uppercase: false,
},

cases: [{ type: mongoose.Schema.Types.ObjectId, ref: "Cases" }],
createdAt: {
    type: Date,
    default: Date.now,
},
});

module.exports = mongoose.model("Vol", VolSchema);

Models / Judge.js

const mongoose = require("mongoose");

const JudgeSchema = new mongoose.Schema({
name: String,
bio: String,
cases: [{ type: mongoose.Schema.Types.ObjectId, ref: "Cases" }],
});

module.exports = mongoose.model("Judge", JudgeSchema);

resolvers.js

const Vol = require("../models/Vol");
const Case = require("../models/Cases");
const Judge = require("../models/Judge");

module.exports = {
Mutation: {
addCase: async (_, { title, volID, judgesID }) => {
  const newCase = await new Case({
    title: title,
    vol: volID,
    judges: judgesID,
  }).save();

  const newVol = Vol.updateOne(
    { _id: volID },
    { $push: { cases: newCase } },
    { new: true },
  );
 const judge =  Judge.updateOne(
    { _id: judgesID },
    { $push: { cases: { $each: [newCase], $position: 0 } } },
    { new: true },
  );

  return newVol, judge // Judge model gets updated only
  return newVol // Vol gets update only
  return{vol: newVol, judges: judge } // i have tried this too
 },
},
};

我希望将newCase推向Vol以及Judge模型

1 个答案:

答案 0 :(得分:0)

这是我最好的答案,因为我不确定您到底在问什么。

const Vol = require("../models/Vol");
const Case = require("../models/Cases");
const Judge = require("../models/Judge");

module.exports = {
Mutation: {
addCase: async (_, { title, volID, judgesID }) => {
  const newCase = await new Case({
    title: title,
    vol: volID,
    judges: judgesID,
  }).save();

  const newVol = Vol.updateOne(
    { _id: volID },
    { $push: { cases: newCase } },
    { new: true },
  );
 const judge =  Judge.updateOne(
    { _id: judgesID },
    { $push: { cases: { $each: [newCase], $position: 0 } } },
    { new: true },
  );

  return newVol, judge // Judge model gets updated only
  return newVol // Vol gets update only
  return{vol: newVol, judges: judge } // i have tried this too
 },
},
};

应该看起来像这样

const Vol = require("../models/Vol");
const Case = require("../models/Cases");
const Judge = require("../models/Judge");

module.exports = {
 Mutation: {
  addCase: async (_, { title, volID, judgesID }) => {
   const newCase = await new Case({
      title: title,
      vol: volID,
      judges: judgesID,
   }).save();

   const newVol = await Vol.updateOne(
      { _id: volID },
      { $push: { cases: newCase } },
      { new: true },
   );
   const judge = await Judge.updateOne(
     { _id: judgesID },
     { $push: { cases: { $each: [newCase], $position: 0 } } },
     { new: true },
   );

   return newCase;
  },
 },
};

此外,因为这是GraphQL,所以如果我知道返回类型,它将有所帮助。我认为返回类型是Case而不是newVol或法官。因此,在这种情况下,您实际上会得到奇怪的结果,而不是返回大小写。在这种情况下,最后一行应显示为return newCase;