flask-mongorest
flask-mongorest copied to clipboard
Using a projection on a dataquery
Hey there,
I have written the following MongoDB schema to return required data, notice the use of a projection on adducts.positive.peaks.
db.metabolites.find(
{ "adducts.positive.peaks.accurate_mass": {$gt : 1200, $lt : 1220}},
{ "name" : 1, "accurate_mass" :1, "adducts.positive.peaks.$": 1}
)
However, there doesn't seem to be any clear way of doing this in flask-mongorest.
Are there any plans to implement the $
operator in flask-mongorest?
Thank you,
Keiron.
Update
I've written a small get_queryset
function in the resource I want to query.
def get_queryset(self, *args, **kwargs):
return super(MetaboliteAdductsResource, self).get_queryset(*args, **kwargs).fields(name=1, accurate_mass=1, molecular_formula=1)
However, I'm struggling a little bit to work out how to add the projection operator into this.
Update 2
Slowly getting there, but I still have a small problem surrounding the fact that the projection still doesn't work as intended.
def get_queryset(self, *args, **kwargs):
return super(MetaboliteAdductsResource, self).get_queryset(*args, **kwargs).fields(name=1, accurate_mass=1,
molecular_formula=1, adducts=1,
adducts_negative_peaks__S = 1
Returns all embedded documents in adducts, despite the obvious projection.
And I've just realised that the projection is incorrect, any help would be greatly appreciated :+1:
Thanks,
Keiron.