flask-mongorest icon indicating copy to clipboard operation
flask-mongorest copied to clipboard

Using a projection on a dataquery

Open KeironO opened this issue 8 years ago • 3 comments

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.

KeironO avatar Jan 16 '17 19:01 KeironO

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.

KeironO avatar Jan 17 '17 16:01 KeironO

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.

KeironO avatar Jan 18 '17 10:01 KeironO

And I've just realised that the projection is incorrect, any help would be greatly appreciated :+1:

Thanks,

Keiron.

KeironO avatar Jan 20 '17 20:01 KeironO