0
每當我嘗試設置新的文檔引用數組時,都會收到錯誤。這導致錯誤說:「無法讀取屬性'$ isMongooseDocumentArray'未定義」;。 我將如何去更新一個文檔,其中包含來自另一個模式的其他文檔的引用數組。我試圖用一個新的集合來替換那個文檔引用的數組。Mongoose:在更新文檔時設置文檔引用的新數組
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);
});