问题描述
我们有一个 MongoDB 副本并读取到 scondaries,在后端我们使用feathersjs 和feathers-mongoose 并且文档有一个子文档。打补丁后,我们取回了旧的子文档。我们已经设置了 new: true
并尝试了 returnNewDocument: true
const params = {
mongoose: {
new: true
}
}
查看feathers-mongoose 的文档告诉我,参数中的mongoose 应该作为mongoose 的选项提供:https://github.com/feathersjs-ecosystem/feathers-mongoose#paramsmongoose
但是 SubDocument 仍然是旧的 id。子文档有什么特别的待办事项吗?
我也对 mongoose 和 mongodb 的文档感到困惑,因为 mongoDB 接缝只知道 returnNewDocument
和 mongoose only new
?
MongoDB:https://docs.mongodb.com/v4.2/reference/method/db.collection.findOneAndUpdate/
猫鼬:https://mongoosejs.com/docs/migration.html#findandmodify-new
架构看起来像:
const timeSchema = new Schema({
weekday: {
type: Number,min: 0,max: 6,required: true,},startTime: { type: Number },duration: { type: Number },eventId: { type: String },room: { type: String },});
const courseSchema = getUserGroupSchema({
description: { type: String },startDate: { type: Date },untilDate: { type: Date },sharetoken: {
type: String,unique: true,sparse: true,// here is the reference
times: [timeSchema],// optional information if this course is a copy from other
iscopyFrom: { type: Schema.Types.ObjectId,default: null },features: [{ type: String,enum: Object.values(COURSE_FEATURES) }],...externalSourceSchema,});
解决方法
简单。
您只需使用 env: sed -i '' '2i\
we are the new 2' /absolute/path/to/file: No such file or directory
函数即可获取更新的文档。
示例:
.save()
您也可以使用 ...
let course = await CourseSchema.findById(id); //let's say
if(!course){
throw new Error(...);
}
course.times.weekday = 5;
course.times.startTime = 9;
let updated = course.save();
console.log(updated); // Updated document will be printed.
...
方法。