Mongoose:更新文档时设置新的文档引用数组

时间:2016-09-12 13:44:00

标签: mongoose mongoose-schema mongoose-populate

每当我尝试设置新的文档引用数组时,我都会收到错误。这会导致错误说明:"无法读取属性' $ isMongooseDocumentArray'未定义&#34 ;; 。 如何使用来自其他模式的其他文档的引用数组更新文档。我试图用新的集合替换那个文档引用数组。

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);
});

1 个答案:

答案 0 :(得分:0)

发现问题,似乎我在调用错误的模式模型-_-