微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mongoose:更新后获取文档的新版本

如何解决Mongoose:更新后获取文档的新版本

我们有一个 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. ... 方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。