猫鼬:更新文档时设置新的文档引用数组
Mongoose: Setting a new array of document references when updating a document
每当我尝试设置新的文档引用数组时,我总是收到错误消息。这会导致错误提示:"Cannot read property '$isMongooseDocumentArray' of undefined";。
我将如何使用对来自另一个模式的其他文档的引用数组来更新文档。我正在尝试用一组新文档替换该文档引用数组。
Lets say I had this schema with a particular reference:
const userCommandSchema = mongo.Schema({
objectID: mongo.Schema.ObjectId,
userID: { type: String, required: true },
command: { type: String, required: true },
summary: { type: String },
isFavourite: { type: Boolean, default: false },
},
{ timestamps: true }
);
const UserCommandSequenceSchema = mongo.Schema({
objectID: mongo.Schema.ObjectId,
userID: { type: String, required: true },
commandSequenceTitle: { type: String, required: true },
commandSequenceDescription: { type: String, required: false },
commands: [{ type: mongo.Schema.Types.ObjectId, ref: 'UserCommand' }],
},
{ timestamps: true }
);
// How would I go about updating an array of document references?
const arrayOfObjectIDs = [mongo.Types.ObjectId("57d2e31f0098c69c4eefde53"), mongo.Types.ObjectId("57d2e31f0098c69c4eefde57")];
const query = { _id: id };
const update = { $set: {
commandSequenceTitle,
commandSequenceDescription,
commands
},
};
const options = { new: true };
UserCommandModel.findOneAndUpdate(query, update, options)
.exec()
.then(({ _id, commandSequenceTitle, commandSequenceDescription, commands, createdAt, updatedAt }) => {
})
.catch((err) => {
console.log(err);
});
发现问题,似乎我调用了错误的模式模型-_-
每当我尝试设置新的文档引用数组时,我总是收到错误消息。这会导致错误提示:"Cannot read property '$isMongooseDocumentArray' of undefined";。 我将如何使用对来自另一个模式的其他文档的引用数组来更新文档。我正在尝试用一组新文档替换该文档引用数组。
Lets say I had this schema with a particular reference:
const userCommandSchema = mongo.Schema({
objectID: mongo.Schema.ObjectId,
userID: { type: String, required: true },
command: { type: String, required: true },
summary: { type: String },
isFavourite: { type: Boolean, default: false },
},
{ timestamps: true }
);
const UserCommandSequenceSchema = mongo.Schema({
objectID: mongo.Schema.ObjectId,
userID: { type: String, required: true },
commandSequenceTitle: { type: String, required: true },
commandSequenceDescription: { type: String, required: false },
commands: [{ type: mongo.Schema.Types.ObjectId, ref: 'UserCommand' }],
},
{ timestamps: true }
);
// How would I go about updating an array of document references?
const arrayOfObjectIDs = [mongo.Types.ObjectId("57d2e31f0098c69c4eefde53"), mongo.Types.ObjectId("57d2e31f0098c69c4eefde57")];
const query = { _id: id };
const update = { $set: {
commandSequenceTitle,
commandSequenceDescription,
commands
},
};
const options = { new: true };
UserCommandModel.findOneAndUpdate(query, update, options)
.exec()
.then(({ _id, commandSequenceTitle, commandSequenceDescription, commands, createdAt, updatedAt }) => {
})
.catch((err) => {
console.log(err);
});
发现问题,似乎我调用了错误的模式模型-_-