【问题标题】:Mongoose: Setting a new array of document references when updating a documentMongoose:更新文档时设置新的文档引用数组
【发布时间】:2017-01-19 23:29:48
【问题描述】:

每当我尝试设置新的文档引用数组时,我都会收到错误消息。这会导致错误提示:"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);
});

【问题讨论】:

    标签: mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    找到问题了,好像我调用了错误的模式模型-_-

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2016-10-06
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多