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

Array query operators #26

Open
art049 opened this issue Oct 26, 2020 · 3 comments · May be fixed by #33
Open

Array query operators #26

art049 opened this issue Oct 26, 2020 · 3 comments · May be fixed by #33
Labels
enhancement New feature or request
Projects

Comments

@art049
Copy link
Owner

art049 commented Oct 26, 2020

Suggested implementation of $elemMatch $all and $size

MongoDB Reference

Example model:

class Thing(Model):
    array: List[int]

$elemMatch

Array containing the number 42 at least once:

Thing.array.any().eq(42)
eq(Thing.array.any(), 42)
Thing.array.any() == 42
42 in Thing.array # if possible using __contains__

$elemMatch

Array containing only elements greater than 12:

Thing.array.all().gt(42)
gt(Thing.array.all(), 42)
Thing.array.all() > 42

$size

Array containing more than 3 elements:

Thing.array.size().gte(3)
gte(Thing.array.size(), 3)
Thing.array.size() >= 3
len(Thing.array) >= 3 # if possible
@art049 art049 created this issue from a note in Development (Next release) Oct 26, 2020
@art049 art049 added the enhancement New feature or request label Oct 26, 2020
@art049 art049 linked a pull request Oct 28, 2020 that will close this issue
@art049 art049 moved this from Next release to In progress in Development Nov 11, 2020
@erny
Copy link
Contributor

erny commented Jan 26, 2021

When will be the next release? I would love to have this feature.

But I really need sequences over EmbeddedModels, and I don't know if I should open a new issue (or there is already one) e.g.:

class EmbeddedAuthor(EmbeddedModel):
    name: str
    year: int

class Book(Model):
    title: str
    authors: List[EmbeddedAuthor]

The query could be something like:

Book.authors.any(EmbeddedAuthor.name == "Frederick Forsyth")

but I would love much more:

Book.authors.any().name == "Frederick Forsyth"

Looking about what MongoDB allows to do, it seems that we can query over arrays with the "any" operator transparently, i.e.:

Thing.array.any() == 42

and

Thing.array == 42

would be the same. This is especially interesting for embedded models:

Book.authors.name == "Frederick Forsyth"

which should read like "Tell me which books have at least one author with name "Frederick Forsyth".

Thanks again for this great piece of software.

@art049
Copy link
Owner Author

art049 commented Mar 15, 2021

Hey @erny. Thanks for your enthusiasm 😄 .
I'm currently working on this feature, release should come soon 😉

I totally agree with the use case you described and it should definitely possible to have it with this feature !

@Osc44r
Copy link

Osc44r commented Apr 10, 2024

Any update on that?

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
Development
  
In progress
Development

Successfully merging a pull request may close this issue.

3 participants