-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add transform
option for populate()
#3775
Comments
blog.find().populate({
path: 'category',
select: 'name category_slug',
match: { category_slug: req.params.category_slug },
}).exec(function(error, data){
console.log(data);
}); The above code translates to:
Most likely, |
@vkarpov15 when populate a path, if the result not match with |
@isayme yep that sounds right |
How to remove that implicit checking of _id matching with category? I am having similar issues. Why is not not taking only the criteria specified in the 'match'. Why taking an inplicit match on _id? Is there a way to solve this? |
@abhishekjain2604 I don't understand, can you please elaborate with code samples? |
These are my 2 models. The flow is that the productModel has a deafult price. But I can also set user specific prices, as specified in the pricesModel. What I want to be able to do it to populate 'price' field in productModel from pricesModel based on which user is making the request to get it's detail. So something like this:
|
That's the problem. In this example, we are populating the path 'fans' which is of the type ObjectId. In my use case, I want to populate the field 'price' which is just a Number. |
In that case, you should consider making let productSchema = new SCHEMA({
defaultPrice: Number
});
productSchema.virtual('price').get(function() {
if (this.userPrice != null) {
return this.userPrice.specificPrice;
}
return this.defaultPrice;
});
productSchema.virtual('userPrice', {
ref: 'Price',
localField: '_id',
foreignField: 'productId',
justOne: true
}); |
That solution would have worked but the 'specific prices' are user specific. In this 'userPrice' virtual example, localField of productSchema matches against the foreign field of priceSchema. But there's another field in priceSchema(user) that needs to be matched against a field that is not present in the productSchema. It will come from the 'request' object inside the controller. request.user.id to be specific. I understand that my use case might be beyond the realms of what a framework needs to be able to do. But does this use case sounds like something that might be helpful enough to be introduced as a native feature? Something that might prove useful to many others as well? |
Perhaps the below will work? productSchema.virtual('userPrice', {
ref: 'Price',
localField: '_id',
foreignField: 'productId',
justOne: true
});
// In route handler
Product.findOne({ _id: request.productId }).populate({ path: 'userPrice', match: { userId: request.user.id } }); See this example. The above code will find the |
Sorry for replying so late that too on my own query. But yes, that did work and greatly reduced the overhead of computing everything manually by querying database especially when retrieving 100s of products at the same time. Thank you so much for your help. |
Is there a way to keep the _id instead of null when the match fails? For example: If I have the field PersonId (which is an Object Id from the model Person) in another model. I do want to populate if this _id is not equal the current logged User, lets call id UserId. so, my query is something like:
when the PersonId is different from UserId it returns the person data, but when it is equal it returns null. I see why is it and it makes sense. But is there a way of avoiding the null result and keep the Object Id when the match fails? |
@Scientistt no but that is a good idea. Will keep this issue open for a future release. |
null
if populate()
finds no results, leaving the original id.
Can we prioritize this? It's a very common use case when implementing populate with virtuals and I wonder why it's not the default behaviour. |
@Doublehub we'll see what we can do for 5.12. The 5.11 milestone is a little crowded right now. |
null
if populate()
finds no results, leaving the original id.transform
option for populate()
The following will work in Mongoose 5.12.0: const parent = yield Parent.findById(p).populate({
path: 'children',
transform: function(doc, id) {
return doc == null ? id : doc;
}
}); Still need to double check that this works with populate virtuals. |
…al populate and conventional populate Fix #3775
My code:
result[1].category : null . Why?
And how to find blog by category_slug?
I'm a beginner
The text was updated successfully, but these errors were encountered: