These are chat archives for Automattic/mongoose

5th
Jul 2018
foureight84
@foureight84
Jul 05 2018 03:54
Is there away to get what schema prop was set in the populate via the pre middleware? I'm trying to write a conditional to trigger another property in schema to autopopulate if a certain property is called to be populated
foureight84
@foureight84
Jul 05 2018 04:09
Also, is there a way to virtual populate a prop that is a nested virtual? let's say company is a virtual. company contains "users", which is an array of ObjectId Referencing the Users collection. Now I have another virtual called "members" and members needs to return populated "company.users". Is that possible?
Kev
@lineus
Jul 05 2018 09:22
@foureight84 To answer your first question, this in query pre-hooks is the Query object. There is a mongoose internal property on the query object that holds the populate object called _mongooseOptions which could be accessed as demonstrated in this gist. There is no guarantee that _mongooseOptions won't change without notice in the future though ( being an internal and all ). A more future-proof way to solve the problem would be to create a static method that conditionally adds paths to the populate string/object. A very simplistic example here
you could also file an issue to request that the _mongooseOptions be formalized as part of the public API instead of being internal. It never hurts to ask :)
Kev
@lineus
Jul 05 2018 09:31
your second question is a little harder to follow, a code sample would be helpful.
Osny Netto
@osnysantos
Jul 05 2018 10:30
hello guys, I'm looking an article which says find + limit is way better in terms of performance than findOne. Is it still true nowadays?
Kev
@lineus
Jul 05 2018 10:33
@osnysantos do you have a collection with enough documents to test both and see a discernible difference? I'd be curious to know the answer.
Osny Netto
@osnysantos
Jul 05 2018 10:35
no, right now my document has about 300 items
I'm trying to get current user, User.find({_id: 12345}).limit(1)
@lineus
Ashish Madeti
@ashishmadeti
Jul 05 2018 10:42
@osnysantos Can you give the link of the article?
I want to know if this is something mongoose specific or is the case with any MongoDB query.
Osny Netto
@osnysantos
Jul 05 2018 10:49
@ashishmadeti
Kev
@lineus
Jul 05 2018 10:56
@osnysantos in the case of mongoose and the mongodb native driver, a findOne call is really just a wrapper around find with .limit set to -1 and batchSize set to 1.
Osny Netto
@osnysantos
Jul 05 2018 10:59
@lineus so no difference?
Kev
@lineus
Jul 05 2018 11:00
nope, cause you're calling .find() under the hood either way :)
it looks like sometime in v2 of the native driver they actually deprecated .findOne and in v3 they kept it but changed it to a wrapper around that .find() call.
Darren Schnare
@dschnare
Jul 05 2018 19:20
hey guys, i have a quick question, I'm trying to determine how Schemas (if at all) are created on the database side. Does mongooose Schema's leverage the $jsonSchema or any query validators when creating the collection? any help would be greatly appreciated
Kev
@lineus
Jul 05 2018 19:21
@dschnare nope, it's all in the node process :)
Darren Schnare
@dschnare
Jul 05 2018 19:21
kool, thanks @kev, that's what I thought, just wanted to be sure I didn't miss anything in the source code
Kev
@lineus
Jul 05 2018 19:22
yeah I looked for it too a while back
foureight84
@foureight84
Jul 05 2018 20:02
@kev Thanks for the confirmation on the use of _mongooseOptions. I will make a post to see if it's possible to make that public. regarding my second question, I am including an example below. The primary issue I have is that the 'team_members' virtual (which uses the '_company_user' virtual) doesn't populate 'team_members' when populate is called. It returns an unpopulated array of ObjectId's.

userSchema ({
    profile: { type: String, default: ''}
})

companySchema ({
    name: {type: String}
})

companyUserSchema ({
    _company: {type: Schema.Types.ObjectId, ref: 'Company'},
    _user: {type: Schema.Types.ObjectId, ref: 'User'},
    team_members: [{type: Schema.Types.ObjectId, ref: 'User'}]
})

userSchema.virtual('_company_user', {
    ref: 'CompanyUser',
    localField: '_id',
    foreignField: '_user'
})

userSchema.virtual('team_members', {
    ref: 'User',
    localField: '_company_user.team_members'
    foreignField: '_id'
})
foureight84
@foureight84
Jul 05 2018 20:23
I've added this question to github as well. Automattic/mongoose#6678