mongoose如何关联保存
发布于 7 年前 作者 huanghaiyang 5648 次浏览 最后一次编辑是 4 年前

我现在又两个model,book和file 如下: var mongoDoc = require(’./db’); var mongoose = mongoDoc.mongoose; var db = mongoDoc.db; // 书籍定义 var BookSchema = mongoose.Schema({ name: String, ename: String, price: String, discount: { type: String, default: ‘10’ }, secDiscount: { type: String, default: ‘10’ }, bookVersion: { type: Number, default: 1 }, isStore: Boolean, publicDate: Date, forwardSaleDate: Date, appriaseState: Number, // 是否推荐 idRecommanded: { type: Boolean, default: false }, // 编辑推荐指数 recommandedExponent: { type: Number, default: 0 }, storeNumber: Number, ISBN: String, pageNumber: Number, banding: Number, memo: String, createDate: { type: Date, default: new Date() }, keyword: String, authors: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘author’ } ], translators: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘translator’ } ], publicCompany: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘publicCompany’ } ], bookCategray: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘bookCategray’ } ], promotion: Boolean, promotionDepart: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘promotionDepart’ } ], images: [{ type: mongoose.Schema.Types.ObjectId, ref: ‘file’ } ] }, { safe: { j: 1, w: 2, wtimeout: 20000 }, capped: { size: 1024, max: 100, autoIndexId: true } }); var BookModel = db.model(“book”, BookSchema); module.exports.BookModel = BookModel; module.exports.BookSchema = BookSchema;

var mongoDoc = require(’./db’); var mongoose = mongoDoc.mongoose; var db = mongoDoc.db; var FileSchema = mongoose.Schema({ url: String, // 使用场景 useType: { type: String }, name: String, memo: String, uploadDate: { type: Date, default: new Date() }, // 是否正在被使用中 isCurrentUsed: { type: Boolean, default: false }, // 文件类型==扩展名 type: { type: String }, filename: String, // mime类型 filetype: String, filesize: String, // 是否可以删除 canRemove: { type: Boolean, default: true }, // 是否可以移动 canChangeFolder: { type: Boolean, default: true }, // 是否可以重命名 canRename: { type: Boolean, default: true }, // 随机码 randomNumber: { type: String, default: Math.random() + ‘’ } }, { safe: { j: 1, w: 2, wtimeout: 20000 }, capped: { size: 1024, max: 100, autoIndexId: true } }); var FileModel = db.model(“file”, FileSchema); module.exports.FileSchema = FileSchema; module.exports.FileModel = FileModel;

操作方法: // 上传对应图书的图片信息 Book.prototype.uploadBookImage = function(bookid, attributes, callback) { var image = new FileModel({ name: attributes.name, memo: attributes.memo, useType: attributes.type, isCurrentUsed: attributes.isCurrentUsed ? attributes.isCurrentUsed : true, uploadDate: new Date(), filename: attributes.file.filename, filetype: attributes.file.filetype, filesize: attributes.file.filesize, filepath: attributes.file.filepath, type: attributes.file.filename.substring(attributes.file.filename.lastIndexOf(’.’) + 1) }); BookModel.findById(bookid) ; // 查找图书信息 BookModel.findById(bookid, function(err, book) { if (err) return callback(err); else { image.save(function(errr, img) { if (errr) { return callback(errr); } else { book.images = [img.id]; BookModel.update(book, { new: true, upsert: false }, function(err, book_) { if (err_) return callback(err_); return callback(err_, book_); }); } }); } }); }

为什么这样不能将file的关联关系保存到book中呢 有什么好的方法么

1 回复

代码太不清晰了,要格式化

回到顶部