我有两个模式,并且每个模式都想返回一个属性。
这是单个文档或文件的架构。
// @ts-check
import { Schema, model } from "mongoose";
const docSchema = new Schema({
name: { type: String, required: true },
approved: { type: Boolean, default: false },
docType: { type: String, required: true },
size: { type: Number, required: true },
bucket: { type: Schema.Types.ObjectId, ref: "Bucket" }
});
docSchema.methods.getSize = function(cb) {
// Return the size of document
};
const Doc = model("Document", docSchema);
export default Doc;
这是一堆文档的架构
// @ts-check
import { Mongoose, Schema, model } from "mongoose";
const bucketSchema = new Schema({
name: { type: String, required: true, index: true, unique: true },
projectId: { type: String, required: true, index: true },
contractorId: { type: String, required: true, index: true },
Docs: [{ type: Schema.Types.ObjectId, ref: "Document" }]
});
bucketSchema.methods.getSize = function(cb) {
// return the size of all documents that belong to a single bucket
// How do I traverse over the Docs array and use Doc.getSize()???
};
const Bucket = model("Bucket", bucketSchema);
export default Bucket;
有没有办法做到这一点?我不需要每次都需要获取存储桶的大小然后遍历结果并添加每个文档的大小时都必须查询数据库。我想简化一下,以便基本上我叫Bucket.getSize()
并返回给我存储桶的大小,以便当用户超过特定限制时可以限制用户上传文件。
任何帮助,将不胜感激。