我想使用一个有效负载将其更新/推送到多个猫鼬模式。例如。我有一个VolSchema
,一个CaseSchema
和一个JudgeSchema
。每当创建新的VolSchema
时,我都希望将值推入JudgeSchema
和Case
。当我仅更新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模型
答案 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;