These are chat archives for Automattic/mongoose

27th
Oct 2016
LG
@componentDidMount
Oct 27 2016 04:04
hi all, is there a posibility to have default schema options? i want these { timestamps: true, minimize: false, useNestedStrict: true, toObject: { getters: true } } options for all my schemas, without defining them in every schema, is there a way to pass them at connect or something else?
i know i could pass them from the env or something but since i now that i always want these in y service i dont even want to pass them at schema instantiation
Paul "Joey" Clark
@joeytwiddle
Oct 27 2016 04:06
Instead of new Schema({...}) it can be quite healthy to call your own createSchema({...}) function.
Then you can always add extra magic to all your schemas at a later date, by modifying your createSchema function.
LG
@componentDidMount
Oct 27 2016 04:07
yay a wrapper, wonderful idea
thanks @joeytwiddle
Paul "Joey" Clark
@joeytwiddle
Oct 27 2016 04:08
You’re welcome.
Mika Fischer
@mika-fischer
Oct 27 2016 08:06
@elgubenis You can also have global plugins, which do something similar: http://mongoosejs.com/docs/plugins.html
Paul "Joey" Clark
@joeytwiddle
Oct 27 2016 08:47
Oh that’s nice. I had only seen them add middleware before. Didn’t realise they could modify the schema too. https://github.com/Santinell/mongoose-allRequired
temuccio
@temuccio
Oct 27 2016 13:41
Hello All
I have one problem with find
I have two docs same this:
{
    "_id" : ObjectId("57dc2b63add5c5e91f185fe0"),
    "level" : 2,
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439011"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 1,
            "pagminuto" : 10,
            "pagmax" : 10,
            "pagmin" : 1
        }, 
        {
            "_id" : ObjectId("507f191e810c19729de860ea"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498c"),
            "costopag" : 0.5,
            "pagminuto" : 10,
            "pagmax" : 100,
            "pagmin" : 1
        }
    ],
}
{
    "_id" : ObjectId("57dc2b63add5c5e91f185dd7"),
    "level" : 2,
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439011"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 2,
            "pagminuto" : 100,
            "pagmax" : 10,
            "pagmin" : 1
        }
    ],
}
I have make this find function:
Utente.find({
          "level":2,
          $and: [{
            "profili.idprofilo": profilo._id,
            "profili.pagmin": {$lte:infofile.numpage},
            "profili.pagmax": {$gte:infofile.numpage}
          }]
        })
Have this problem:
temuccio
@temuccio
Oct 27 2016 13:47
When profilo._id is 57fe3c51569b5c0afb88498b and infofile.numpage is 3
The result of find is "_id" : ObjectId("57dc2b63add5c5e91f185fe0") and it is correct
When infofile.numpage is 14
The result of find is "_id" : ObjectId("57dc2b63add5c5e91f185fe0") and "_id" : ObjectId("57dc2b63add5c5e91f185dd7")
but the first id of this results is unattended!!!
why I have this problem?
temuccio
@temuccio
Oct 27 2016 14:08
I suppose that I do usage .populate() call, it is right?
chaconv
@chaconv
Oct 27 2016 15:16
for (var d = firstDay; d <= lastDay; d.setDate(d.getDate() + 1)) { dayOfWeek = d.getDay(); weekOfMonth = getWeekOfMonth(d); Recurrent.find({onDay : dayOfWeek, onWeek: weekOfMonth}) .then( function (doc){ console.log(d); }); }
I have a loop that does a query for every date in a year. But after mongoose does this async query I need the date that i used on every call to use it on my callback... But the value is allways the last value of the loop (it finishes before the first callback comes)
Paul Barrick
@peb7268
Oct 27 2016 20:00
What is the best way to fetch data from multiple different models in one request? Say for instance, I have a University login portal and when the admin logs in the app needs to fetch all students, classes, and teachers for an initial bootstrap? Do you have to nest each of those model queries and return your response from the deepest one or is there a better way?