These are chat archives for Automattic/mongoose

22nd
Jan 2016
Willem D'Haeseleer
@helmus
Jan 22 2016 04:52
Guys, what up with these v2 query streams, anyone writing any code to make that happen ?
Nejc Vukovic
@nvwebd
Jan 22 2016 10:54
quick question: how do I connect two Schemas from two diff files? so that I can access a schema between the files? I’ll be creating n-n connections between collections.
J
@jhyland87
Jan 22 2016 17:33
Hey guys. Is there a way to reference models within model methods?
Like if in ModelA, I wanted to create a method that would return the results for a query, but for each result, attach a query result from ModelB to each document
I have my models split up into different files
And loaded with this index
Should I require that index inside each model? (And have it exclude the current model)?
Actually, I guess I can just require the necessary ones..
J
@jhyland87
Jan 22 2016 18:11
anyone there?
This message was deleted
This message was deleted
Valeri Karpov
@vkarpov15
Jan 22 2016 18:53
@helmus no progress that I know of. Contributions welcome but I'm hoping to get that done within the next 3-6 months
@nVVEBd export one schema with module.exports, require() the schema in the other file
@jhyland87 sounds like you're trying to build your own populate()... http://mongoosejs.com/docs/populate.html
Nejc Vukovic
@nvwebd
Jan 22 2016 19:56
@vkarpov15 thanks for the info. It's that easy? damn :)
antonioaltamura
@antonioaltamura
Jan 22 2016 20:03

Hi guys, I'm trying to add a function to a Mongoose schema. This is part of my app.js (i'm on express.js)

PersonSchema.statics.search = function search (name) {
return 'name:'+name
};
app.get('/site/', function(req, res) {
console.log(PersonSchema.search('test'))
res.send( '');
});

What am I doing wrong?

I get the error "PersonSchema.search is not a function"
Nejc Vukovic
@nvwebd
Jan 22 2016 20:07
you could console the PersonSchema for starters to see if the function exists. If I remember correctly you have to you need to use the model constructor first for the functions to be there. try this
app.get('/site/', function(req, res) {
var testModel = new PersonSchema(req.body);
console.log(testModel.search('test'));
res.json(testModel);
});
antonioaltamura
@antonioaltamura
Jan 22 2016 20:21
PersonSchema it's a mongoose schema, not a model, so with your code I get "PersonSchema is not a function", anyway very thanks for the help!:)
var Schema = mongoose.Schema;
var PersonSchema = new Schema({
    confirmed: { type: Boolean, required: true, default: false },
    name: {
        first: { type: String, required: true, forms: {
            new: {},
            edit: {}
        }},
        last: { type: String, required: true, forms: {
            new: {},
            edit: {}
        }}
    }
});
(the same error "PersonModel is not a function" if I use the model)
I'm sure I'm using statics in the wrong way, it's like there is no visibility of the method in app.get(..)
J
@jhyland87
Jan 22 2016 21:04
@vkarpov15 that looks like it might be it.. Thanks!
@vkarpov15 I see where it explains how to do them. But in a Relational DB, if I wanted to do a many-to-many relation of say.. accounts to groups. I would use a pivot table. In NoSQL, you don't create pivot tables (Right?), so is it best to have the group ID's referenced within the accounts collection, or should the documents in the groups collection reference the account ID's that belong to said group?
J
@jhyland87
Jan 22 2016 21:10
My guess is to reference the group ID's in the documents within the accounts collection, because that way, when you delete an account, you dont have to go through the groups and update each document, removing the account ID... is that right?
antonioaltamura
@antonioaltamura
Jan 22 2016 23:33
About the previous problem, maybe this example is more clear, Dog.methods.let_cat_speak works fine, Dog.statics.sayHello doesn't work!:
var Schema = mongoose.Schema;
var Dog = new Schema({
    name: { type: String, required: true },
});
Dog.statics.sayHello = function() {
    console.log("Wow wow");
};
Dog.methods.let_cat_speak = function() {
    console.log("meow");
};
var dog_model = mongoose.model('form', Dog);
app.get('/site/', function(req, res) {
   var dog=new dog_model({name:"charlie"})
   dog.let_cat_speak();
   res.send('');
});