I have website configuration (currently stored as JSON file), and I would like to move it to MongoDB and probably use Mongoose to handle read-write operations and perform validation through schemas.
Configuration is an object with limited amount of keys, similar to that:
{
siteOffline: false,
storeOffline: false,
priceMultipliers: {
a1: 0.96
a2: 0.85
},
...
}
Should it be made a collection with key-value entries? Not sure how to enforce Mongoose schema in this case.
Should it be made a collection with a single document? Not sure how to guarantee that there is only one document at a time.
Any other options?
Ok, one thing at a time :
if you want to use mongoose, you should have your full config as one document :
var siteConfig = new Schema({
siteOffline: Boolean,
storeOffline: Boolean,
priceMultipliers: {
a1: Number
a2: Number
}
});
Then, if you want a one document only collection, you can use MongoDB capped collections
I suggest you go through the multiple options that Mongoose allows, here
The Schema for your one-doc collection would be something like :
var config = new Schema({
siteOffline: Boolean,
storeOffline: Boolean,
priceMultipliers: {
a1: Number
a2: Number
}
}, {
collection:'config',
capped: { size: 1024, max: 1}
});
There's some irony in having a "collection" of only one document though :)
Another "hack" solution that could work better for you is to use a secured field (of which you cannot change the value), and add a unique index on that field. Read this for more info The field could be present in the document but not in the model (virtual). Again, this is hacky, but your use case is a bit strange anyway :)