These are chat archives for Automattic/mongoose

10th
Feb 2016
Christopher Dunavan
@HyperSprite
Feb 10 2016 00:43
@alfredopacino , have you looked at restify-mongoose , it basically takes your models and builds your routes for you.
antonioaltamura
@antonioaltamura
Feb 10 2016 13:04
@HyperSprite yeah I've just looking to figure it out how to make it works :)

guys I worried about a particular schema I need (array of embedded document with ref in it)

var docSchema = new Schema({
    name:String
    embedded_ref:[{ref:{type: ObjectId, ref: 'charterArticles' },comment:String}],
})
var charterArticleSchema = new Schema({
    name:{type:String,required:true},
    chapter:{type:String,required:true}
}, { collection: 'charterArticles' });

how can I make a find() query that search for all docs that contains a specific charterArticle name or chapter?

Saurabh Vyas
@saurabhvyas
Feb 10 2016 14:58
If I want to update all records using $in and passing an array as query what is the method for that?
Chris Paradeise
@cparadeise
Feb 10 2016 14:59
db.collection.update({field:{$in:<theArray>}},{$set:{field:'someValue'}},{multi:true})
that's the gist of it
Saurabh Vyas
@saurabhvyas
Feb 10 2016 15:00
@cparadeise thank you!
Chris Paradeise
@cparadeise
Feb 10 2016 15:00
@saurabhvyas no problem! :D
I think multi is necessary here, could be worth it to try it without and see what happenes
Saurabh Vyas
@saurabhvyas
Feb 10 2016 15:01
yeah the doc says by default it only updates one document but once we specify multi:true we can update multiple docs
I meant documentation
Chris Paradeise
@cparadeise
Feb 10 2016 15:02
Gotcha, I was just wondering if it would act different because of the $in
I've not done such a thing before
Learning new things always :P
Saurabh Vyas
@saurabhvyas
Feb 10 2016 15:02
oh yeah me too :)
benjfield
@benjfield
Feb 10 2016 15:58
hello
im trying to work out how to do a query on an array of objects
so
i have an object like { "_id": "56bb398d3a65f112306f0a50", "__v": 0, "teams": [ { "team": "56ab9b2424ed5c46e8c76baf", "winner": true, }, { "team": "56ab9b2424ed5c46e8c76bb0", "winner": false, } ] } and i want to count how many entries have the right team and are winners
This message was deleted
benjfield
@benjfield
Feb 10 2016 16:03
i know i can check just the right team - Game.count({ "teams.team": id })
say
benjfield
@benjfield
Feb 10 2016 16:53
got it, use $elemMatch
J
@jhyland87
Feb 10 2016 16:55

Hey guys. Im looking for a better way to structure my methods so I can use something like JSDoc-readme and/or jsdoc to generate some documentation and readmes for the models... I was thinking keeping the static methods and instance methods in arrays, then loading them like so:

_.forEach( ['findByName','getDisabledUsers'], m => {
    models.statics[ m ] = staticMethods[m]
})

_.forEach( ['restoreDoc','modifyDoc'], m => {
    models.methods[ m ] = instanceMethods[m]
})

Has anyone done that before?

Then atleast I can generate the documentation and readme based off of the file containing all the real methods...
I just have a lot of models and methods within the models, and they're changing frequently... its a pita
I document my code well enough, so Id like to automate generating the readme and docs if I can
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:43

Hey guys! Just a question, I hope this is the right forum.

If I have a document with an array of sub-docs, and I want don't want it to be populated until I say so, using the populate() method. (Since I believe it can be very big after a while). What is the way to properly structurize the schema? I'll paste my schema, but I think the proper way is by using {type: Schema.Types.ObjectId, ref: somethingsomething}

Pastebin: http://pastebin.com/9GcR6E0K

Note that the subdocuments never will be created on their own.
Konstantin Baierer
@kba
Feb 10 2016 17:46
@patrikmolsson Sure, that seems reasonable. populate isn't called automatically, it's your choice when to populate and when not to
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:48
The thing is, it seems to be populated already if using the code in the pastebin...
@kba What I understand is that if I use the {type: Schema.Types.ObjectId...} way as I mentioned, it won't be populated until I say so, but I can't get it to work with that approach....
Konstantin Baierer
@kba
Feb 10 2016 17:51
ok, if you want to use the subdocument/populate approach, you need to do [{type: ObjectId, ref: something...}]
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:51
@kba Alright, and the
'something' needs to reference a model?
Konstantin Baierer
@kba
Feb 10 2016 17:52
The name of a Model or a Schema
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:53
The name of the Schema, not the reference to the Schema itself?
Konstantin Baierer
@kba
Feb 10 2016 17:54
The 'Story' in var Story = mongoose.model('Story', storySchema);
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:55
It works perfectly! :) Thank you @kba !
Konstantin Baierer
@kba
Feb 10 2016 17:56
yay :) you're welcome
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:56
I earlier used the reference to the model, not the name of the model itself... Silly me.
@kba Another question, is the "var Story = " part necessary?
Konstantin Baierer
@kba
Feb 10 2016 17:57
Well, if you wan't to do something like inserting or searching Stories, then yes.
Or what do you mean?
Patrik Olsson
@patrikmolsson
Feb 10 2016 17:59
@kba I never use it other than user.receipts.push({name: newName})
@kba Actually, the populate, doesn't work now. When I get the user, the receipts array holds all the id references, but when I use the populate function, those disappears... I will dig som more.
Konstantin Baierer
@kba
Feb 10 2016 18:02
You did register the receiptSchema with mongoose? mongoose.model('Receipt', receiptSchema)?
sry, gotta go good luck
Patrik Olsson
@patrikmolsson
Feb 10 2016 18:03
@kba Yup. Alright, thanks for your help!
J
@jhyland87
Feb 10 2016 18:09
@vkarpov15 Im looking for a better way to structure my methods so I can use something like JSDoc-readme and/or jsdoc to generate some documentation and readmes for the models... I was thinking keeping the static methods and instance methods in arrays, then loading them like so:
_.forEach( ['findByName','getDisabledUsers'], m => {
    models.statics[ m ] = staticMethods[m]
})

_.forEach( ['restoreDoc','modifyDoc'], m => {
    models.methods[ m ] = instanceMethods[m]
})
Is that feasible?