I am working on migrating stats calcul from php to node.js. The previous develloper made one collection per client, and all collection does not follow the same schema.
However, the part I need for calcul are the same on all collection, so what I am trying to do is a "uncomplete" schema like this:
S_logs.web = new mongoose.Schema({
/*
user_ip : String,
user_id : String,
user_agent : String,
*/
canal_id : String,
theme_id : String,
video_id : String,
time : Number,
/*
action : String,
is_newuser : String,
operator : String,
template : String,
catalogue : String,
*/
referer : String,
/*
from : String,
request : String,
smil : String,
smil_path : String
*/
}, {
collection: db_web
});
M_logs.web = mongoose.model(db_web, S_logs.web, db_web);
The commented fields are field that only exist (under that name or form) only in one collection. Uncommented fields are thoses I need and exist in all collection.
If all fields are uncommented, everythings works, but when I comment out thoses fields, it seems the collections isn t recognized, even thought I gave the name of the collection.
Am I missing a flag/option that allow me for such schema? Or am I doomed to make "all fields" schema for each clients?
EDIT:
When I speak of "uncomplete" schema, it s more "select only thoses fields I know exist in the middle of all I don t know", I do not need to put data in the collection, that's why I find odd that MongoDB doesn t have anything like that.
Mongoose can act in a "schemaless" mode. There are some consequences such as missing accessors for each of the fields and needing to specify those "explicitly" though:
S_logs.web = new Schema({}, { strict: false });
M_logs.web = mongoose.model( db_web, S_logs.web, db_web );
So that means when you access a "field/property" in your document you need to use the .get()
method instead of a autogenerated accessor, such as :
M_logs.web.find({}, function(err,logs) {
logs.forEach(function(log) {
var canal_id = log.get("canal_id");
// do something with that value
});
});
This is useful if all documents do not conform to a schema pattern that is consistent, but understand that the auto-generated objects cannot have the same convenient interface when dealing with this level of "polymorphism" as it is not something supported by mongoose.