Skip to content
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

Fetch specific fields in a query #64

Open
skyox opened this issue Dec 5, 2020 · 9 comments
Open

Fetch specific fields in a query #64

skyox opened this issue Dec 5, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@skyox
Copy link

skyox commented Dec 5, 2020

Protection queries are a particular type of MongoDB queries where you can specify fields you want to get in the output.

Another example where the projection parameter is used:
db.writers.find( { "author": "Gaurav Mandes" }, { _id:0, author:1, title:1 } )
In the example above, the _id field is excluded, which automatically gets added, and the title and author fields are displayed.

@skyox skyox changed the title How to use specify fields in query How to get specify fields in query Dec 5, 2020
@art049
Copy link
Owner

art049 commented Dec 6, 2020

Yes, the beginning of an implementation has been done in #1 .
But in the meantime, after some thoughts I ended up thinking that it would be better to use mongo db views to that end.
The usage with odmantic would be something like this:

class Rectangle(Model):
    length: int
    width: int

LengthView = createView(Rectangle, (Rectangle.length,))
# LengthView is a View object derived from pydantic; containing only the length field
rects = await engine.find(LengthView, Rectangle.width > 10)

On top of this, since we are using mongodb view, it would be as well possible to do something like this:

class RectangleAreaView(View, model=Rectangle):
    area = Rectangle.length * Rectangle.width

This would create a View containing a field computed (db side) from some other of its fields.

@art049 art049 added the enhancement New feature or request label Dec 6, 2020
@skyox
Copy link
Author

skyox commented Dec 26, 2020

Thank you so much

@art049 art049 changed the title How to get specify fields in query Fetch specific fields in a query Jan 16, 2021
@amr3k
Copy link

amr3k commented Mar 2, 2021

Sorry, I don't get it!! where is the createView function? how do I use it?

@art049
Copy link
Owner

art049 commented Mar 2, 2021

It's not there yet but it's in the plans :)

@Azrael-git
Copy link

Hello, any updates about this issue? This functionality is very promising I think

@JiaoyangDong
Copy link

I second this. I use this features in mongo/mongoose a lot. Would be a very nice feature to add. :)

@fqzhang42
Copy link

Any plan to implement this?

@ILugaro
Copy link

ILugaro commented Jun 24, 2024

We need it)

@syedzahed
Copy link

Need it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants