当前位置: 动力学知识库 > 问答 > 编程问答 >

database schema - Store a Mongoose scheme in a scheme

问题描述:

I'm using Mongoose to store requests made to an API. I use this data entry to hold the progress of the request in a queuing system so I would like them all to be stored in a single collection for easy lookup.

I would like to create a model to represent the generic request headers.

var ApiRequest = new Schema({

route: String,

priority: String,

maxResponseAge: String,

progress: String,

timestamp: { type: Date, default: Date.now },

request: UnknownMongooseSchema

});

I want the 'request' to be an unknown mongoose scheme (it depends on the API request that was made). For example here's a couple of the request types.

var UrlRequest = new Schema({

urls: String,

followredirects: Boolean

});

var TweetRequest = new Schema({

user: String,

pass: String,

tweet: String

});

Does anyone know how to store a random Scheme in a Scheme?

网友答案:

Forget that original idea... I wrote it in haste.

Mongoose's schema object constructor takes an object literal as its first parameter. Bring the object literal out of the constructor and then extend the object literal with your additional fields/schema's of choice.

var schema = {
    field: String,
    anotherField: Boolean,
}

schema.aCollection = [anotherSchema];
var APIRequestSchema = new Schema(schema);

schema.aCollection = [yetAnotherSchema];
var OtherRequestSchema = new Schema(schema);
分享给朋友:
您可能感兴趣的文章:
随机阅读: